问题
Help with Response.TransmitFile
Before posting, I searched on here and found several similar threads like this one
Trouble with Response.WriteFile / Response.BinaryWrite / Response.TransmitFile (ASP.NET)
Where the solution was to include Response.End();
Well, I have included that last little line, but still nothing is happening.
I've even included an <asp:Label ID="lblErrMsg" runat="server" />
in the body of the ASPX page, but nothing is writing to it either.
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;
}
}
}
- Does anyone see what I've done wrong?
Like I said, lblErrMsg.Text
is blank, but no download dialog is showing up and nothing appears to be happening.
UPDATE:
I'm working with Aristos. I have modified my method as follows using his suggestions, but my sample download file still does not show up:
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;
}
回答1:
Try to understand that you have ONE pipeline that you return back to the browser data.
In you code you act like you send back two kind of data, one to the page and one to the download data - but you actually can't do that. Is like you try to write on two different files - having one stream open to one file.
So select ether send the file, ether update the page.
I have describe here what I think that is best way to download a file:
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)
来源:https://stackoverflow.com/questions/14760154/response-transmitfile-nothing-happening