Search code examples
c#asp.netexcelpackage

Server can download excelpackage file but client can't


I was trying to create excelpackage download function. When i try it on my server or on my local development server, the download button works and it sends Excel file to the browser. But after i deployed and try to download from client, it returns nothing.

Result using Chrome

Result using Chrome

Result using Firefox

enter image description here

Here is my code :

if (Response.IsClientConnected)
    {
        Response.Clear();
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;  filename=" + departmentName + ".xlsx");
        Response.BinaryWrite(excelPackage.GetAsByteArray());
        Response.Flush();
        Response.Close();
    }

I tried to follow the instructions that were provided here,

Microsoft Office Excel cannot access the file 'c:\inetpub\wwwroot\Timesheet\App_Data\Template.xlsx'

But still won't fix my problem.


Solution

  • if (Response.IsClientConnected)
    {
        Response.Clear();
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;  filename=" + departmentName + ".xlsx");
        Response.BinaryWrite(excelPackage.GetAsByteArray());
        Response.Flush();
        Response.End();
    }
    

    Somehow when i change

    Response.Close()
    

    to

    Response.End()
    

    It works.