简体   繁体   中英

Gridview with download and view pdf files in asp.net & c#

Everything is working fine, until the pdf file should be able to be clicked n viewd on the browser.Though the download link is working perfectly.

My grid...

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText = "No files uploaded">
<Columns>
    <asp:BoundField DataField="FileDate" HeaderText="Dated" />
    <asp:BoundField DataField="FileName" HeaderText="File Name" />
    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%#   Eval("FilePath") %>' runat="server" OnClick = "DownloadFile" ></asp:LinkButton>
             <asp:LinkButton ID="btnOpen" Text="View" Font-Bold="true" runat="server" onclick="btnpdf_Click" /></asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
        </Columns>
    </asp:GridView>

My class

public class Thing
{

    public string FilePath { get; set; }
    public string FileName { get; set; }
    public string FileDate { get; set; }
}

My PageLoad

 string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
        List<Thing> lst = new List<Thing>();
        foreach (string filePath in filePaths)
        {
            lst.Add(new Thing() 
            { 

                //FileDate = File.GetCreationTime(filePath).ToShortDateString(),
                FileDate = Path.GetFileName(filePath.Substring(0,35)),
                FileName = Path.GetFileName(filePath.Substring(36)), 
                FilePath = filePath 
            });
        }
        GridView1.DataSource = lst;
        GridView1.DataBind();

My download button

string filePath = (sender as LinkButton).CommandArgument;
    Response.ContentType = ContentType;
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath.Substring(36)));
    Response.WriteFile(filePath);
    Response.End();

My pdfview

string filePath = (sender as LinkButton).CommandArgument;
    Response.ContentType = "Application/pdf";
    //Get the physical path to the file.
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath.Substring(36)));
    //Write the file directly to the HTTP content output stream.
    Response.WriteFile(filePath);
    Response.End();

Still i cannot view the pdf on a browser...pls help

 <asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%#Eval("notice")%>' runat="server" onclick = "lnkDownload_Click" ></asp:LinkButton>
        &nbsp;&nbsp;
         <asp:LinkButton ID="btnOpen" Text="View" CommandArgument='<%#Eval("notice")%>' Font-Bold="true" runat="server" onclick = "btnOpen_Click"></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>
    protected void lnkDownload_Click(object sender, EventArgs e)
    {
        string filePath = (sender as LinkButton).CommandArgument;
        Response.ContentType = ContentType;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath.Substring(0)));
        Response.WriteFile(filePath);
        Response.End();
    }

    protected void btnOpen_Click(object sender, EventArgs e)
    {
        string filePath = (sender as LinkButton).CommandArgument;
        Response.ContentType = "Application/pdf";
        //Get the physical path to the file.
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
        // Write the file directly to the HTTP content output stream.
        Response.Redirect(filePath);
        Response.End();

    }

对于pdfview-将Content-Disposition更改为inline而不是附件

Response.AppendHeader("Content-Disposition", "inline; filename=" + Path.GetFileName(filePath.Substring(36)));

Instead of opening PDF file in other browser, open that in a pop up. In pop up add a iframe and give your PDF file path to it. No need to move to New window. You can preview in same window and when you give PDF file as a source to iframe it will by default give options to save and print. Good luck.!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM