2

I written the below c# code for downloading the attachments in my application, when i run the code i can download the files using Mozilla, internet explorer but this is not working in Google Chrome.

string base64FileString = result;
byte[] binaryFile = Convert.FromBase64String(base64FileString);
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", String.Format("attachment; filename=\"{0}\"", Request.QueryString["FILENAME"]));
Response.Clear();
Response.BufferOutput = false;
Response.ClearContent();
Response.BinaryWrite(binaryFile);
Response.Flush();
Response.Close();

Can anyone please help me what changes need to do for downloading in Chrome

3per
  • 351
  • 9
  • 26
Triandh Velchuri
  • 191
  • 2
  • 3
  • 10

3 Answers3

4

Google Chrome will not open a "file save" window for you if "application/octet-stream" is your response from a form.

The content-type should be whatever it is known to be, if you know it. E.g. "application/pdf", "image/png" or whatever. See Complete list of MIME types

See this question What could keep Chrome from downloading files? for more information.

Community
  • 1
  • 1
George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
  • See also [how to determine a file's MIME type from its file name extension](http://stackoverflow.com/questions/1029740/get-mime-type-from-filename-extension) – John Wu Oct 12 '16 at 16:42
  • 1
    I used "application/pdf" but still i am getting the same error. – Triandh Velchuri Oct 12 '16 at 17:32
  • when i was downgraded my google chrome to older version then the above code is working fine.Is anyone know what changes we need to do for the above code for downloading the file in newer versions of Google Chrome. – Triandh Velchuri Oct 16 '16 at 03:46
  • Remove `Response.Close();` See https://stackoverflow.com/a/736462/481207 – Matt Nov 12 '17 at 21:26
0

You can try adding Content-Length header, but not sure it's effective:

System.IO.FileInfo fl = new System.IO.FileInfo(downloadFilePath);
this.HttpContext.ApplicationInstance.Context.Response.AddHeader("Content-Length", fl.Length.ToString());
yu yang Jian
  • 6,680
  • 7
  • 55
  • 80
-1

This same issue dreaded me for months. Here is how I solved it.

  1. Open IIS Manager
  2. Click on the active server for which your site is running
  3. In the "Feature View" scroll down and locate Configuration Editor under "Server Component".
  4. Under the configuration editor section, there is a dropdown. Go through it and look for system.applicationHost
  5. Under it, you should find weblimits
  6. The configuration to change is minBytePerSecond. the default value may be 240. Microsoft recommends 500 but this issue of download occurs when user is in a region of slow internet connectivity. The fix is setting the value to 0. There may be a side effect to this because it may open your service to slow loris attack

NOTE: Per what i have read on the internet so far, i believe this fix is application to IIS > 7. I mine was IIS 10 so the steps above is for IIS 10.

Hope it helps somebody.

OneGhana
  • 119
  • 5