在发布之前,我在这里搜索并发现了几个类似这样的线程
Trouble with Response.WriteFile / Response.BinaryWrite / Response.TransmitFile (ASP.NET)
解决方案包括Response.End();
好吧,我已经包含了最后一点,但仍然没有发生任何事情。
我甚至在ASPX页面的正文中包含了<asp:Label ID="lblErrMsg" runat="server" />
,但没有任何内容写入它。
public void DownloadFile(string nameOnly) {
if (!String.IsNullOrEmpty(nameOnly)) {
lblErrMsg.Text = "Transfer request for " + nameOnly;
string filename = Server.MapPath(nameOnly);
try {
if (!String.IsNullOrEmpty(filename)) {
Response.Buffer = true;
Response.Clear(); // clear the buffer
Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (-1 < filename.IndexOf(".avi")) {
Response.ContentType = "video/x-msvideo";
lblErrMsg.Text = "Content Type Set to Video.";
} else if (-1 < filename.IndexOf(".pdf")) {
Response.ContentType = "Application/pdf";
lblErrMsg.Text = "Content Type Set to PDF.";
} else if (-1 < filename.IndexOf(".rar")) {
Response.ContentType = "Application/x-rar-compressed";
lblErrMsg.Text = "Content Type Set to RAR.";
} else {
Response.ContentType = "Application/octet-stream";
lblErrMsg.Text = "Content Type Set to Octet Steam.";
}
FileInfo file = new FileInfo(filename);
Response.AddHeader("Content-Disposition", "attachment; filename=" + nameOnly);
Response.AddHeader("Content-Length", file.Length.ToString());
lblErrMsg.Text = "Content Headers added.";
Response.TransmitFile(file.FullName);
lblErrMsg.Text = "Transfer Completing...";
Response.End();
lblErrMsg.Text = "Transfer Complete.";
} else {
throw new Exception(string.Format("Server was unable to locate file \"{0}\".", nameOnly));
}
} catch (Exception err) {
lblErrMsg.Text = err.Message;
}
}
}
就像我说的那样,lblErrMsg.Text
是空白的,但是没有出现下载对话框,似乎没有任何内容发生。
更新
我正在和Aristos合作。我使用他的建议修改了我的方法如下,但我的样本下载文件仍未显示:
private string Download(string nameOnly) {
string outputLine = null;
if (!String.IsNullOrEmpty(nameOnly)) {
string filename = Server.MapPath(nameOnly);
if (!String.IsNullOrEmpty(filename)) {
try {
Response.Buffer = false;
Response.Clear(); // clear the buffer
Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (-1 < filename.IndexOf(".avi")) {
Response.ContentType = "video/x-msvideo";
} else if (-1 < filename.IndexOf(".pdf")) {
Response.ContentType = "Application/pdf";
} else if (-1 < filename.IndexOf(".rar")) {
Response.ContentType = "Application/x-rar-compressed";
} else {
Response.ContentType = "Application/octet-stream";
}
FileInfo file = new FileInfo(filename);
Response.AddHeader("Content-Disposition", "attachment; filename=" + nameOnly);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.TransmitFile(file.FullName);
} catch (Exception err) {
outputLine = err.Message;
} finally {
Response.Flush();
}
} else {
outputLine = string.Format("Server was unable to locate file \"{0}\".", nameOnly);
}
}
if (String.IsNullOrEmpty(outputLine)) {
Response.Redirect("~/Default.aspx");
}
return outputLine;
}
答案 0 :(得分:1)
尝试了解您有一个管道返回浏览器数据。
在你的代码中,你的行为就像你发回两种数据,一种是页面,一种是下载数据 - 但实际上你不能这样做。就像你试图写两个不同的文件 - 一个流打开一个文件。
因此选择ether发送文件,以太网更新页面。
我在这里描述了我认为这是下载文件的最佳方式:
Side actions not happening after Response.WriteFile()
What is the best way to download file from server
Error handling when downloading file from ASP.NET Web Handler (.ashx)