【问题标题】:How to display pdf file in aspx page using asp.net control?如何使用asp.net控件在aspx页面中显示pdf文件?
【发布时间】:2017-11-24 08:02:46
【问题描述】:

我需要在点击按钮时在 aspx 页面内显示 pdf 文件(使用 asp.net 控件)。

我目前已执行以下代码,该代码在 Web 浏览器中显示 pdf 文件。

string folderPath = 
    Server.MapPath(ConfigurationManager.AppSettings["serverFolderPath"].ToString());
foreach (string file in Directory.GetFiles(folderPath))
{
    WebClient user = new WebClient();
    Byte[] fileBuffer = user.DownloadData(file);

    if (fileBuffer != null)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-length", fileBuffer.Length.ToString());
        sponse.BinaryWrite(fileBuffer);
    }
}  

【问题讨论】:

标签: c# asp.net pdf


【解决方案1】:

如果您的文件夹路径正确,请尝试以下代码 它的工作

        string strDirectoryPath = @"C:\Users\UserDesktopName\Desktop\";
        WebClient User = new WebClient();
        foreach (string strFilePath in Directory.GetFiles(strDirectoryPath))
        {
            string strFileExtension = Path.GetExtension(strFilePath);
            if (strFileExtension == ".pdf")
            {
                Byte[] FileBuffer = User.DownloadData(strFilePath);
                this.Response.ContentType = "application/pdf";
                this.Response.AppendHeader("Content-Disposition;", "attachment;filename=" + FileBuffer);
                this.Response.WriteFile(strFilePath);
                this.Response.End();
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    相关资源
    最近更新 更多