86001

Filename and mime problems - ASP.NET Download file (C#)

Question:

I'm facing a very strange problem in my ASP.NET Application.

<strong>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</strong> (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 <strong>application/x-zip-compressed</strong>, <strong>multipart/x-zip</strong> and of course <strong>application/zip</strong>. With application/zip Internet Explorer 8 shows XML error.

Any help with be very appreciated.

Greetings,

Answer1:

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.

<strong>Edit</strong>: Also see this SO question <a href="https://stackoverflow.com/questions/1384649/how-to-retrive-and-download-server-files-file-exists-and-url" rel="nofollow">How to retrieve and download server files (File.Exists and URL)</a>

<strong>Update</strong>: Based on your feedback, i think <a href="https://stackoverflow.com/questions/848679/reading-a-binary-file-and-using-response-binarywrite" rel="nofollow">this</a> is what you're looking for.

Answer2:

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.

Answer3:

For Excel export

Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", fileName));

It worked for me with IE and Firefox.

Recommend

  • C# Response Shows Download Unsuccessful Error on Android Browser
  • asp.net reload page after export to excel
  • How to Open Powerpoint file in Browser using asp.net
  • Exporting datatable to excel file with save dialog
  • Response.Clear and Response.ClearContent in Spring MVC
  • download option in repeater table
  • How to prevent css missing when exporting some html to excel file?
  • Android: Adding Layouts Dynamically By Inflating XML
  • Why are files are different when downloading from an ASP.NET (AJAX download with Blob)
  • How to make a real-time jquery effect?
  • How to stream byte array image?
  • Can not cast IMAPInputStream to Multipart
  • how to parse HTTP POST(file upload) stream?
  • How to download attachment from gmail in C# using IMAP?
  • TextBox controls are not Working with Export To PDF(iTextSharp)
  • How to update the UI immediately when a new record is added? Related to ember-cli-pagination
  • Trouble downloading file from browser on Android
  • SOAP with Attachment / MIME content
  • HttpRequestMessage Content Disposition null when unit testing
  • Fixed Background Works in Chrome but Not Firefox?
  • Angular2 Response for preflight is invalid (redirect) from some GET requests
  • Selenium to click on a javascript button corresponding to a text
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • Simulate click Geckofx vb,net
  • IE7 and TinyMCE with Plone
  • How to view images from protected folder with php?
  • Moving Android View and preventing onDraw to be called over and over again
  • Assign variable to the value in HTML
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • How to have background script and something similar to a default popup?
  • How to use carriage return with multiple line?
  • How to attach a node.js readable stream to a Sendgrid email?
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Align navbar back button on right side
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Perl system calls when running as another user using sudo
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API