0

Thank You for reading my post. I have written a HttpResponse code to download a file. Following the code:

            string fullPath = Path.Combine(appPath, randomFileName);
            File.WriteAllBytes(fullPath, data);

            HttpResponse response = HttpContext.Current.Response;
            response.Clear();
            response.ClearContent();
            response.ClearHeaders();
            response.BufferOutput = true;
            response.Cache.SetCacheability(HttpCacheability.Private);
            response.CacheControl = "private";
            response.AddHeader("Content-Type", "binary/octet-stream");
            response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\";");
            response.AddHeader("Content-Length", data.Length.ToString((IFormatProvider)CultureInfo.InvariantCulture));
            response.Flush();
            response.BinaryWrite(data);
            response.Flush();
            response.End();

Here, the File.WriteAllBytes method creates a file in the server, this is an xlsx file. Upon opening this file in notepad++, the file has 1506791 characters. When opening the downloaded xlsx file in notepad++ the file has 1490407 characters.

The downloaded file doesnot open in Microsoft Excel. I used notepad++ to see if all the bytes were downloaded or not. Apparently, all the bytes are not downloaded.

HttpResponse is not downloading all the bytes.

Is it possible that the data.Length method does not count unicode chars and when response.BinaryWrite is executed, the binarywrite does and stops when data.Length value reached which is not the end of the byte.

austin
  • 45
  • 1
  • 11
  • Have you tried using the debugger to see what the value of `data.Length` is? – itsme86 Nov 09 '17 at 21:14
  • 2
    A little hard to keep track with all those Clear() and Flush() calls. – H H Nov 09 '17 at 21:20
  • @itsme86 The `data.Length` is as same as the length of the file written by `File.WriteAllBytes`. `data.Length` returns correct value. – austin Nov 09 '17 at 21:24
  • Have you tried replacing "attachment" with "inline" in your Content-Disposition header? e.g. - response.AddHeader("Content-Disposition", "inline; filename=\"" + filename + "\";"); –  Nov 09 '17 at 23:07
  • [Avoid using `Response.End()`](https://stackoverflow.com/questions/1087777/is-response-end-considered-harmful). – John Wu Nov 10 '17 at 00:49

1 Answers1

0

This approach uses the file you save right before sending the same data back to the client (let me know how it goes):

        string fullPath = Path.Combine(appPath, randomFileName);
        File.WriteAllBytes(fullPath, data);

        HttpResponse response = HttpContext.Current.Response;

        response.ClearContent();
        response.ClearHeaders();
        response.AddHeader("Content-Disposition", "inline; filename=" + randomFileName);
        response.ContentType = "binary/octet-stream";
        response.WriteFile(fullPath);
  • Thank You but when I used this code with `response.Flush()` at the end, the file didn't download and the message was `Failed Network` – austin Nov 10 '17 at 16:30
  • Have you tried it just as it is above (i.e. - No Flush, End, etc.)? –  Nov 10 '17 at 16:56
  • Yes, it didn't download anything. Then I added flush which tried to download. – austin Nov 10 '17 at 17:24
  • What about with adding only "response.End();" as the last line in my suggestion? (no Flush, etc.); –  Nov 10 '17 at 17:50
  • When adding `response.End()` after `response.WriteFile(fullPath)`, the download starts but just keeps circling and has message `Starting...`. Does not go past this. – austin Nov 10 '17 at 19:03