Thursday, April 24, 2014

Excel download and open error from portal

Once we download excel file from portal and try to open file the below error will be shown.

The code used for the download is as follows.
HttpContext.Current.Response.TransmitFile(filepath);
System.IO.File.Delete(filepath);
HttpContext.Current.Response.End();

Here the issue comes because the file will be deleted immediately once the file transmitted. During this time the original file will be locked by other process.

So changing the code as below will resolves the issue.

HttpContext.Current.Response.TransmitFile(filepath);
HttpContext.Current.Response.Flush();
System.IO.File.Delete(filepath);
HttpContext.Current.Response.End();