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.