【发布时间】:2016-02-02 11:05:02
【问题描述】:
我正在尝试在客户端计算机上下载文件。在客户不知情的情况下,我不会尝试这样做。该文件已存储在服务器上。我有文件路径,我尝试了下面的方法。但这似乎不起作用。我正在使用当前版本的 Google Chrome。而且一直失败。
try{
string Filpath = ParseRequest.GetFileDetails(fileId);
FileInfo file = new FileInfo(Filpath);
string fileLength = file.Length.ToString();
string fileName = file.Name;
string fullName = file.FullName;
if (file.Exists)
{
Response.ClearContent();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Length", fileLength);
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.Write(fullName);
Response.Flush();
Response.Close();
}else{
Response.Write("This file does not exist.");
}
}catch(Exception ex){
XLog.Error(ex);
}
【问题讨论】:
-
“它似乎不起作用”不足以帮助我们帮助您诊断此问题。是否抛出异常?是否记录了错误?它在其他浏览器中是否有效?你有没有通过设置断点和观察代码在做什么来调试这个?
-
你不能通过下载来驱动。那是令人难以置信的糟糕品味。
-
对于 PDF,您需要
Response.BinaryWrite()和Response.ContentType = "application/octet-stream";(或者只是.WriteFile()) -
@Cᴏʀʏ 通过提到它“似乎不起作用”,我的意思是我得到了响应,但是当我在“开发人员工具”上检查响应时,它就像一个解析的 PDF 垃圾值字符串(按 F12)。
-
@DanielA.White 你能详细说明一下“下载驱动”吗?
标签: c# asp.net google-chrome