3

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

Example

Veljko89
  • 1,813
  • 3
  • 28
  • 43
  • Are you using Webforms? You don't need to use `context.Response` or `Response` in any other stack. In ASP.NET MVC, Web API, Core, you return a file with the `File()` method – Panagiotis Kanavos Jan 24 '19 at 11:33
  • 1
    As for the actual error `Excel foun unreadable content in 'filename.xlsx'. Do you want to recover ....` it has nothing to do with ASP.NET or TransmitFile. It means the file is corrupt, or that it isn't an Excel file at all. How did you create that file? Is it a real `xlsx` file or an HTML/CSV file with a fake extension and content type? – Panagiotis Kanavos Jan 24 '19 at 11:35
  • To create a *real* `xlsx` file use a library like EPPlus or the Open XML SDK directly to create it `xls` is obsolete, replaced by XLSX back in 2006, 13 years ago. – Panagiotis Kanavos Jan 24 '19 at 11:37
  • @PanagiotisKanavos I am using jQuery to call my service which creates file and should return response (file to download), I am using OfficeOpenXML (which saves and create .xlsx file on server correctly, I do see file on my server) ... so better question is how to return file from server to app, should i use response and write array of bytes to output stream .. or as attachment within response – Veljko89 Jan 24 '19 at 12:08
  • What does *attachment within response* mean? HTTP has not concept of "attachments" – Liam Jan 24 '19 at 13:36
  • @Liam using WCF – Veljko89 Jan 24 '19 at 13:38
  • How about using File.ReadAllBytes(filepath) to get the bytes data of your excel, and use Response.BinaryWrite(bytes) to write back to client, is it the same error? – Ackelry Xu Jan 25 '19 at 07:13
  • @AckelryXu tried that, first report download was full report (30kb), second download was error (1kb) ... – Veljko89 Jan 25 '19 at 09:23

0 Answers0