Gridview,可在 asp.net 和C#中下载和查看pdf文件

本文关键字:下载和 pdf 文件 可在 asp net Gridview | 更新日期: 2023-09-27 18:30:35

一切

正常,直到pdf文件应该能够在浏览器上点击和查看。虽然下载链接运行良好。

我的网格...

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

我的班级

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

我的页面加载

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

我的下载按钮

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

我的文件夹视图

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...请帮助

Gridview,可在 asp.net 和C#中下载和查看pdf文件

 <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 - 将内容处置更改为内联而不是附件

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

不要在其他浏览器中打开PDF文件,而是在弹出窗口中打开它。在弹出窗口中添加一个 iframe 并提供您的 PDF 文件路径。无需移动到新窗口。您可以在同一窗口中预览,当您将PDF文件作为iframe的源时,默认情况下它将提供保存和打印选项。祝你好运。!