Click here to Skip to main content
15,844,364 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to export crystal report from asp.net web API IhttpActionResult, the report gets exported but is corrupt and cannot be repaired after download. however, the same report when exported to disk if fine and usable. please how do i achieve this, thank you.

What I have tried:

IHttpActionResult response;
           HttpResponseMessage responseMsg = new
           responseMsg.Content = new ByteArrayContent(new StreamContent(rd.ExportToStream(ExportFormatType.PortableDocFormat)).ReadAsByteArrayAsync().Result);
           responseMsg.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
           responseMsg.Content.Headers.ContentDisposition.FileName = "Print.pdf";
           responseMsg.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

           return response;
Posted
Updated 15-Jul-20 0:51am

1 solution

Don't use .Result to force an asynchronous operation to run synchronously. You can just make your action async and await the operation.

But in this case, you don't need the ByteArrayContent at all. Just use the StreamContent directly.
C#
responseMsg.Content = new StreamContent(rd.ExportToStream(ExportFormatType.PortableDocFormat), 1024 * 1024);
 
Share this answer
 
Comments
Enobong Adahada 15-Jul-20 7:18am    
Thank you, Richard, for the prompt response, I have tried out your solution and I get the same error when I have downloaded the file and attempt to open it, "there was an error opening this document, the file is damaged and could not be repaired.
Richard Deeming 15-Jul-20 7:25am    
At a guess, you might need to reset the position of the stream after exporting:
Stream pdfStream = rd.ExportToStream(ExportFormatType.PortableDocFormat);
if (pdfStream.CanSeek) pdfStream.Seek(0L, SeekOrigin.Begin);
responseMsg.Content = new StreamContent(pdfStream, 1024 * 1024);

Otherwise, you'll need to open up the downloaded file in a binary editor to see whether it looks OK.
Enobong Adahada 15-Jul-20 8:29am    
Thank you again, I have tried this, and it gives the same issues.
Richard Deeming 15-Jul-20 8:41am    
Then you'll need to open up the downloaded file in a binary editor to see what the content looks like.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900