繁体   English   中英

Gridview与asp.net和C#中的下载和查看pdf文件

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

一切工作正常,直到可以在浏览器中单击该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();

我的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 ... pls帮助

 <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-将Content-Disposition更改为inline而不是附件

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM