<分区>
使用以下代码下载 PDF 文件时出现问题:
WebClient client = new WebClient();
client.DownloadFile(remoteFilename, localFilename);
虽然其他文件已成功下载,但当我下载并保存 PDF 文档时,当我尝试打开它时显示文档已修复错误。
<分区>
使用以下代码下载 PDF 文件时出现问题:
WebClient client = new WebClient();
client.DownloadFile(remoteFilename, localFilename);
虽然其他文件已成功下载,但当我下载并保存 PDF 文档时,当我尝试打开它时显示文档已修复错误。
最佳答案
检查这个方法,希望有帮助
public static void DownloadFile(HttpResponse response,string fileRelativePath)
{
try
{
string contentType = "";
//Get the physical path to the file.
string FilePath = HttpContext.Current.Server.MapPath(fileRelativePath);
string fileExt = Path.GetExtension(fileRelativePath).Split('.')[1].ToLower();
if (fileExt == "pdf")
{
//Set the appropriate ContentType.
contentType = "Application/pdf";
}
//Set the appropriate ContentType.
response.ContentType = contentType;
response.AppendHeader("content-disposition", "attachment; filename=" + (new FileInfo(fileRelativePath)).Name);
//Write the file directly to the HTTP content output stream.
response.WriteFile(FilePath);
response.End();
}
catch
{
//To Do
}
}
关于c# - 为什么我下载文件的代码会生成损坏的 PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4644506/