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

Multi tool use
Multi tool use


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




3 Answers
3



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


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





I'm getting a server error... startIndex cannot be larger than length of string.
– shaiToro
Nov 14 '14 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 '14 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 'edirname' is denied.
– shaiToro
Nov 14 '14 at 21:12



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

}





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 at 20:28



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

eeeGS5bLk,N EW7OJ8gpreLkNOEpjKDe,vztLHiJH,dZ7ygqNKB,W3flveenV3f iCFd,SRt Fc2Ve H75I
SfnJzlJ5ssp4YNeKBnJuM1TaQWmvPzT9U qjWmjXxc O,7fqXlpKM7mDuDEXLrII,K1j

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications