I'm trying to create a file handler for users to download files when their filenames are clicked on a web page. I've implemented this a few times without issues, but I'm currently getting an error which I can't get my head around.
Code:
protected void btnViewFile_Click(object sender, EventArgs e)
{
var btnViewFile = sender as LinkButton;
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + btnViewFile.CommandArgument.ToString());
Response.WriteFile(Server.MapPath(btnViewFile.CommandArgument));
Response.End();
}
If I look at the browser console, I can see:
Uncaught Sys.WebForms.PageRequestManagerParserErrorException: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.
No exceptions appear to be thrown in the code, the requested file is converted into the correct full path; and I've tried quite a few different things - clearing headers manually, flushing before ending, giving a more explicit content-type header, using AddHeader instead of AppendHeader, using TransmitFile rather than WriteFile, and quite a bit more.
Any ideas?
In case anyone else comes across this situation, the problem was that I was registering it as a postback control in ScriptManager, not an async postback control.
D'oh!
Related
I have a code set that runs on the server, which correctly generates a zip file and stores it on the server. I have that file location as a physical path.
Nothing I have attempted has allowed me to use the response to the client to download that file.
Attempt 1:
System.IO.FileInfo fi = new System.IO.FileInfo(zipFilePath);
//setup HTML Download of provided Zip.
//application/zip
Response.ClearContent();
Response.Clear();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application / zip";
Response.AddHeader("Content-Disposition",
"attachment; filename=\"" + System.IO.Path.GetFileName(zipFilePath) + "\";");
Response.AddHeader("Content-Length", fi.Length.ToString());
Response.TransmitFile(zipFilePath);
Response.Flush();
Response.End();
No result. Code executes without error but there is no download to the client.
Attempt 2:
//Almost the same as attempt 1, but with WriteFile instead
Response.WriteFile(zipFilePath);
No Result, same as Attempt 1.
Attempt 3:
//Note: Same Header Section as Attempts 1 and 2
System.IO.BinaryReader reader = new System.IO.BinaryReader(new System.IO.FileStream(zipFilePath, System.IO.FileMode.Open));
int CHUNK = 1024;
List<byte> FileArray = new List<byte>();
while (reader.BaseStream.Position < reader.BaseStream.Length)
FileArray.AddRange(reader.ReadBytes(CHUNK));
byte[] bArray = FileArray.ToArray();
reader.Close();
Response.OutputStream.Write(bArray, 0, bArray.Length);
Response.Flush();
Response.End();
No Result, Same as previous attempts
Attempt 4:
//Identical to Attempt 3, but using BinaryWrite
Response.BinaryWrite(bArray);
No Result, Same as previous Attempts.
The Question
Every one of these code blocks runs with no error, But The Save File dialog NEVER appears. I get nothing at all. I cannot figure out for the life of me what I might be missing.
The File Path has been verified as correct
The Code is running on the server, not on the client, I cannot use the 'WebClient.Download' method for this reason
If anyone has any suggestions, I'm all ears. I have no idea how to get this file to download to the client.
I tested your code (attempt 1) and got it working fine with a test file. If the file path would be wrong, you'd get an System.IO.FileNotFoundException so that's probably not the issue.
A couple of ways to address this:
Try inspecting the webpage in, for example, Chrome by right-clicking
and choose inspect. Then click on Network tab, and refresh the
page (where you're supposed to get the file). Check the response
headers for that request - what is it?
Try setting content-type to application/octet-stream
Use debugger in Visual Studio and step through.
This turned out to be an Ajax related error causing issues between UpdatePanels and POST Responses.
The issue was fixed on the page load of the page by adding the call
ScriptManager.GetCurrent(Page).RegisterPostBackControl(btnGenerate);
Im using ssrs through a reports server to generate a resultStream byte array using ReportExecutionService.Render() which I am currently serving to the user with the following code. Is there a way I can use this same byte array to automatically open the report in a new browser window instead of going to the save/open dialog?
public void RenderReport (byte[] reportDigits, ReportItem reportItem)
{
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ContentType = reportItem.ReportMimeType;
response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", reportItem.ExportName));
response.OutputStream.Write(reportDigits, 0, reportDigits.Length);
response.End();
}
In the past I have used a separate ReportViewer.aspx page that I would open first then display the report but would like to do it all in code behind if that is possible.
Thanks
It's this line:
response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", reportItem.ExportName));
Thats causing it to be downloaded. Comment out that line, and as long as the browser can handle the mime type, it will render in the browser window.
Simply change the Header that you are adding to something other than an attachement. Make it the format of your data--and hopefully the browser will recognize it.
I have an aspx (say 1.aspx) page from where first I am downloading a pdf file and then I want to redirect to some Thanks.aspx page. The code is this:
protected void btnSubmit_Click(object sender, EventArgs e)
{
string pathId = string.Empty;
if (Page.IsValid)
{
try
{
pathId = hidId.Value;
DownloadPDF(pathId);
Response.Redirect("Thanks.aspx");
}
catch (Exception ex)
{
throw ex;
}
}
}
protected void DownloadPDF(string pathId)
{
if (!(string.IsNullOrEmpty(pathId)))
{
try
{
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + pathId + ".pdf");
string path = ConfigurationManager.AppSettings["Pdf_Path"].ToString() + "\\" + pathId.Trim() + ".pdf";
Response.TransmitFile(path);
}
catch (Exception ex)
{
throw ex;
}
finally
{
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
The problem is that, the file save dialog is coming properly and I am able to download the file also, but it is not getting redirected to the Thanks.aspx page.
How to resolve this?
If the file is just downloaded, no preprocessing is done, you could try the following:
Response.AddHeader("Refresh", "12;URL=nextpage.aspx");
Where the number is the seconds before refresh is done :)
I've found it easier to put the PDF download page in an iframe. That way you can activate the PDF download on the client side by just pointing the iframe source to the PDF download page. After that you can either move to a new page, or just show the thank you text right that on the page that has the iframe in it.
In HTTP, a request can only have a single response. Since the first response is the PDF file, the seconds response (i.e. the redirect) cannot be implemented.
You can try to redesign the two pages by redirecting to thanks.aspx and have thanks.aspx start the download automatically.
A Response.Redirect actually sends a response to the browser that basically says this resource has moved to some other URL. However, you're trying to send a file down in a response too, so those two things are probably clashing with each other. Try sending back a little JavaScript that sends them to the page you want to send them too instead of using a Response.Redirect.
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "redirectScript", "window.location.href='whateverurlhere.aspx';", True)
See the article mentioned in this accepted answer: https://stackoverflow.com/a/11018277/1037864
(direct link: http://gruffcode.com/2010/10/28/detecting-the-file-download-dialog-in-the-browser/)
The idea is to set a cookie and send it together with the file. Meanwhile you let the waiting page block the UI while it is waiting for the cookie to arrive.
I tried many things (like the ideas here) but nothing worked for my particular situation. In the end for me I used an approach where my C# sets a cookie that the JavaScript looks for and the form buttons/etc are disabled accordingly until the cookie is detected.
My code is here in case anyone thinks this solution might work for you:
https://gist.github.com/cemerson/9811a384d7f41bc683b2bd9ed4bf5b17
When the user clicks a button, I want to build a string, then have the user download that string as a file (it's a CSV file).
var response = HttpContext.Current.Response;
response.ClearContent();
response.Clear();
byte[] bytes = Encoding.ASCII.GetBytes(csvtext);
using (var stream = new MemoryStream(bytes))
{
response.AddHeader("Content-Disposition", "attachment; filename=somefile.csv");
response.AddHeader("Content-Length", stream.Length.ToString());
response.ContentType = "text/plain";
stream.WriteTo(response.OutputStream);
}
This is what I have so far, and I have a feeling I'm pretty close, but I get the following error message:
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near '[The first bit of the CSV file]'.
I'm at a loss, and a deadline is fast approaching. Any help is greatly appreciated.
My guess is that you are in an AJAX Update Panel. If you are doing this type of action it must be done via a postback.
You can't do this with a partial/async postback with Ajax. You'll need to make whatever button you have triggering this download a PostBackTrigger for your UpdatePanel. Details here: http://www.asp.net/Ajax/Documentation/Live/mref/T_System_Web_UI_PostBackTrigger.aspx
I'm facing a very strange problem in my ASP.NET Application.
When the user clicks the button that downloads a file, Internet Explorer / Chrome / Firefox shows the save dialog but the name of the file is the name of the ASPX Page (For example, if the page is named Download.aspx the download dialog shows the "file" Download.zip). Sometimes, when playing with MIME type the download dialog shows "Download.aspx". Seems that you're trying to download the page, but actually is the correct file.
This happens with ZIP extension and here is my code (pretty standard I think):
this.Response.Clear();
this.Response.ClearHeaders();
this.Response.ClearContent();
this.Response.AddHeader("Content–Disposition", "attachment; filename=" + file.Name);
this.Response.AddHeader("Content-Length", file.Length.ToString());
this.Response.ContentType = GETCONTENTYPE(System.IO.Path.GetExtension(file.Name));
this.Response.TransmitFile(file.FullName);
this.Response.End();
The GetContentType function just returns the MIME for the file. I tried with application/x-zip-compressed, multipart/x-zip and of course application/zip. With application/zip Internet Explorer 8 shows XML error.
Any help with be very appreciated.
Greetings,
I'm looking at what I've done to handle a similar mechanism, and here's the steps I'm doing (bold item seemingly the only real difference):
Response.Clear();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // Excel 2007 format
// ... doing work...
Response.AddHeader("Content-Length", outputFileInfo.Length.ToString());
Response.TransmitFile(outputFileInfo.ToString());
HttpContext.Current.Response.End(); // <--This seems to be the only major difference
Although this.Response and HttpContext.Current.Response should be the same, it may not be for some reason.
I think something like Response.Redirect(ResolveUrl(file.FullName)) instead of Response.TransmitFile(file.FullName) is what you intended. It sounds like you actually want their browser to point at the file, not just transmit the file as a response to the current the request.
Edit: Also see this SO question How to retrieve and download server files (File.Exists and URL)
Update: Based on your feedback, i think this is what you're looking for.
For Excel export
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", fileName));
It worked for me with IE and Firefox.