0

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

3 Answers 3

2
 <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();

    }
1
  • Welcome to SO! Just a code snippet is not a very good answer. Please try to explain why this works and what the asker did wrong.
    – Azsgy
    Apr 13, 2018 at 20:28
1

For the pdfview - change the Content-Disposition to inline instead of attachment

Response.AppendHeader("Content-Disposition", "inline; filename=" + Path.GetFileName(filePath.Substring(36)));
3
  • I'm getting a server error... startIndex cannot be larger than length of string.
    – shaiToro
    Nov 14, 2014 at 21:04
  • That looks like a problem with your filePath.Substring(36) - if your filepath is smaller than 36 chars. No idea why you put that in to be honest.
    – Haedrian
    Nov 14, 2014 at 21:06
  • its just a test...just need to extract the name of the file since filename is prepended with a date. I did remove the substring part but ...Access to the path 'e\dirname\' is denied.
    – shaiToro
    Nov 14, 2014 at 21:12
1

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.!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.