我需要在ASPX页面内显示PDF文件(使用ASP.NET Control)在按钮上单击。
我当前已执行以下代码,该代码在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);
}
}
如果您的文件夹正确,请在以下代码下尝试它的工作
string strDirectoryPath = @"C:UsersUserDesktopNameDesktop";
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();
}
}