Reputation: 137
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
Upvotes: 0
Views: 16867
Reputation: 61
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.!
Upvotes: 1
Reputation: 21
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%#Eval("notice")%>' runat="server" onclick = "lnkDownload_Click" ></asp:LinkButton>
<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();
}
Upvotes: 2
Reputation: 4328
For the pdfview - change the Content-Disposition to inline instead of attachment
Response.AppendHeader("Content-Disposition", "inline; filename=" + Path.GetFileName(filePath.Substring(36)));
Upvotes: 1