0

I'm having some problems dealing with downloading a file from the server.
The problem is that the end of the downloaded file is missing.

I have found some indications of other people having similar problems, but nothing that helps me in my problem.

When debugging I have learned that the length of fileData is correct, and all data is present in the byte array when calling BinaryWrite.
That leaves the BinaryWrite, Flush or Close calls... I have read about not using Response.End or Response.Close, for example here:
HttpResponse.End vs HttpResponse.Close vs HttpResponse.SuppressContent and it seems like a probable cause, but what should I use instead (have tried to remove Response.Close completely, but that result in too much data output)?

Someone knows what might cause this behaviour, and how to fix it?

EDIT: Just tried with Response.ContentType = "binary/octet-stream"; and it works like a charm!
What is the difference between text/plain and binary/octet-stream that may cause a behavior like this?
It even works without the Close call...

MORE EDIT: Seems like compression of Responses was activated on the server-side. Apparently there seems to be an issue with plain text streams when compression is active.

The code I have is:

private void DownloadFile(byte[] fileData, string fileName, string fileExtension)
{
    Response.Clear(); 
    Response.AddHeader("content-disposition", "attachment; filename=" + fileName + fileExtension);
    Response.AddHeader("Content-Length", fileData.Length.ToString(CultureInfo.InvariantCulture));
    Response.ContentType = "text/plain";
    Response.BinaryWrite(fileData);
    Response.Flush();
    Response.Close();
}
Community
  • 1
  • 1
Falchion
  • 25
  • 7
  • Response.Flush(); Response.Close(); Response.End(); – sertsedat Mar 24 '16 at 10:37
  • Tried it, but no dice... Now sure how adding a call to `End` would help, since it has already failed on `Close`... And in my case `End` and `Close` would produce the same since I have nothing more to process after the call. – Falchion Mar 24 '16 at 10:54
  • hmm, maybe your file is too big. can you try this way?(in the accepted answer) http://stackoverflow.com/questions/37650/how-to-implement-a-file-download-in-asp-net – sertsedat Mar 24 '16 at 11:16
  • I managed to find what was causing it... compression of responses was activated on the server-side. This probably caused the stream to fail somehow... – Falchion Mar 24 '16 at 11:39
  • @jackjop Thanks for time and effort! – Falchion Mar 24 '16 at 11:46

1 Answers1

0

If compression is active on the server-side (IIS) it apparently causes trouble with text/plain streams.

For you with similar problems, try deactivating, it might help!

It surely did for me!

Falchion
  • 25
  • 7