If you could explain me why TransmitFile ain't working, code is simple, I create excel file (.xlsx
), save it on server and return file path (via CreateProjectsReport), and then I just want to take file and transmit it to user ....
string filePath = CreateProjectsReport(ProjectIds, items);
System.IO.FileInfo file = new System.IO.FileInfo(filePath);
if (file.Exists)
{
HttpContext context = HttpContext.Current;
try
{
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.AddHeader("Content-Disposition",
"attachment; filename=" + file.Name);
context.Response.AddHeader("Content-Length", file.Length.ToString());
// context.Response.ContentType = "application
// vnd.openxmlformats-officedocument.spreadsheetml.sheet";
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.TransmitFile(file.FullName);
context.Response.Flush();
// context.Response.WriteFile(file.FullName, false);
}
catch (Exception ex)
{
LogException(ex);
}
finally
{
System.IO.File.Delete(filePath);
context.Response.End();
}
}
Result of this code is that file get saved on my server in expected folder (full report is there, I verified excel file that is being generated by method) and on response.Flush()
I see file being transmitted on browsers with content length of 30kb
as the file length is on server, but once i click save and file gets saved in my download folder it's length is 10kb
, opening it with excel I get error: Excel foudn unreadable content in 'filename.xlsx'. Do you want to recover ....
.
Checking IIS MIME Types, it's set (I tried using both for content type, get same error):
.xls - application/vnd.ms-excel
.xlsx - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Does context.Response.TransmitFile suppress my file and doesn't send full content of it? Why does this happen?
EDIT
Through browsers console I can see that in Response.Header
property content-length
is exact length of file, but once i click Save
file, that file size goes from 1kb to 15kb tops, instead of 30kb which is on server (it's like TransmitFile(file.FullName)
doesn't Transmit whole file