Gridview,在asp.net&;中下载和查看pdf文件;c#
一切都很正常,直到pdf文件可以在浏览器上点击查看。尽管下载链接工作正常 我的网格Gridview,在asp.net&;中下载和查看pdf文件;c#,c#,asp.net,pdf,gridview,C#,Asp.net,Pdf,Gridview,一切都很正常,直到pdf文件可以在浏览器上点击查看。尽管下载链接工作正常 我的网格 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText = "No files uploaded"> <Columns> <asp:BoundField DataField="FileDate" HeaderText="Dated" /> <
<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>
我的页面负载
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();
我的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();
我仍然无法在浏览器上查看pdf…请为pdfview提供帮助-将内容配置更改为内联而不是附件
Response.AppendHeader("Content-Disposition", "inline; filename=" + Path.GetFileName(filePath.Substring(36)));
对于pdfview-将内容配置更改为内联而不是附件
Response.AppendHeader("Content-Disposition", "inline; filename=" + Path.GetFileName(filePath.Substring(36)));
受保护的无效lnkDownload_单击(对象发送方,事件参数e)
{
字符串filePath=(发送方为LinkButton).CommandArgument;
Response.ContentType=ContentType;
Response.AppendHeader(“内容处置”、“附件;文件名=“+Path.GetFileName(filePath.Substring(0)));
Response.WriteFile(文件路径);
Response.End();
}
受保护的无效btnOpen\u单击(对象发送方,事件参数e)
{
字符串filePath=(发送方为LinkButton).CommandArgument;
Response.ContentType=“Application/pdf”;
//获取文件的物理路径。
AppendHeader(“内容处置”、“附件;文件名=“+Path.GetFileName(filePath));
//将文件直接写入HTTP内容输出流。
重定向(文件路径);
Response.End();
}
受保护的无效lnkDownload_单击(对象发送方,事件参数e)
{
字符串filePath=(发送方为LinkButton).CommandArgument;
Response.ContentType=ContentType;
Response.AppendHeader(“内容处置”、“附件;文件名=“+Path.GetFileName(filePath.Substring(0)));
Response.WriteFile(文件路径);
Response.End();
}
受保护的无效btnOpen\u单击(对象发送方,事件参数e)
{
字符串filePath=(发送方为LinkButton).CommandArgument;
Response.ContentType=“Application/pdf”;
//获取文件的物理路径。
AppendHeader(“内容处置”、“附件;文件名=“+Path.GetFileName(filePath));
//将文件直接写入HTTP内容输出流。
重定向(文件路径);
Response.End();
}
不要在其他浏览器中打开PDF文件,而是在弹出窗口中打开该文件。
在弹出窗口中添加一个iframe并给出它的PDF文件路径。无需移动到新窗口。您可以在同一窗口中预览,当您将PDF文件作为源提供给iframe时,默认情况下,它将提供保存和打印选项。
祝你好运 不要在其他浏览器中打开PDF文件,而是在弹出窗口中打开该文件。 在弹出窗口中添加一个iframe并给出它的PDF文件路径。无需移动到新窗口。您可以在同一窗口中预览,当您将PDF文件作为源提供给iframe时,默认情况下,它将提供保存和打印选项。
祝你好运 我收到一个服务器错误。。。startIndex不能大于字符串的长度。这似乎是filePath.Substring(36)的问题—如果filePath小于36个字符。老实说,我不知道你为什么要把它放进去。这只是一个测试…只需要提取文件名,因为文件名前面有一个日期。我确实删除了子字符串部分,但…对路径“e\dirname”的访问被拒绝。我收到一个服务器错误。。。startIndex不能大于字符串的长度。这似乎是filePath.Substring(36)的问题—如果filePath小于36个字符。老实说,我不知道你为什么要把它放进去。这只是一个测试…只需要提取文件名,因为文件名前面有一个日期。我确实删除了子字符串部分,但是…对路径“e\dirname”的访问被拒绝。欢迎使用SO!仅仅一个代码片段并不是一个很好的答案。请尝试解释为什么这样做,以及提问者做错了什么。欢迎来到SO!仅仅一个代码片段并不是一个很好的答案。请试着解释为什么这样做,以及提问者做错了什么。
Response.AppendHeader("Content-Disposition", "inline; filename=" + Path.GetFileName(filePath.Substring(36)));
<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();
}