We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Unreadable content error after downloading Excel file from Response streams

Hi Aspose Team,

I am using Aspose.Cells of version 17.1.0 with a license file. I have written REST end point which returns a excel file. For that I am loading workbook using template and loading data into it and returning it as a attachment. following is the code

        var binPath = System.Web.Hosting.HostingEnvironment.MapPath("~/bin");
        
        var stream = new FileStream(binPath + "//MyTemplate.xlsx", FileMode.Open, FileAccess.Read);
    
        var loadOptions = new LoadOptions(LoadFormat.Xlsx);	        
        var workbook = new Workbook(stream, loadOptions);
        stream.Close();

        var cells = workbook.Worksheets[0].Cells;
           
        cells.ImportCustomObjects(data.Collection, data.Fields, false, 11, 0, data.Collection.Count,
            true, "mm/dd/yyyy", true);

        var memStream = new MemoryStream();	       
        workbook.Save(memStream, SaveFormat.Xlsx);            
        memStream.Position = 0;
        response.Content = new StreamContent(memStream);
        

        response.Content.Headers.ContentDisposition =
            new ContentDispositionHeaderValue("attachment") { FileName = data.FileName + (data.ViewType == ViewTypeEnum.PDF ? ".pdf" : ".xlsx") };

        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        response.Content.Headers.ContentLength = memStream.Length;

        response.StatusCode = HttpStatusCode.OK;

            return response;

And the strange part is this is working fine locally but when I deploy it to QA environment excel gets corrupted. I tried to load data locally which is there in QA environment but it still works fine locally.

I have seen your other post where people reported same issue and tried following things

  1. workbook.Save(memStream, new OoxmlSaveOptions(SaveFormat.Xlsx));
  2. memStream.Seek(0, SeekOrigin.Begin); & memStream.Close();

But no luck.

Please advise, what could be the reason.

@manasshahms,

Thanks for providing us details.

We are not sure why you are finding this issue on QA server (where it is working fine on your local environment), we do not know the exact reason of the issue. Your sample code also looks fine to us. However, you may just try to send some static data to the client instead of the memorystream data generated by Aspose.Cells to see whether it can work, you should use System.IO APIs with Response object to try to send the file onto the client end and do not involve Aspose.Cells APIs on QA server to check it out.

We also think that regarding your issue, it has nothing to do with Aspose.Cells. The issue should be caused by the process of the streams, such as, the memory stream or response stream, etc.

By the way, could you try to add Response.End at the end of your code segment if it makes any difference.

Thanks @Amjad_Sahi

Issue is resolved. Reason behind this issue was web API MessageHandler which was intercepting all the calls and converting binary content to string content. And we were loading this MessageHandler only for QA environment and not for Dev.

@manasshahms,

Good to know that your issue is sorted out now. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.