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;
}