0

This is my code so far to download the powerpoint file. I used aspose package for powerpoint this is the link to the aspose documentation https://docs.aspose.com/dashboard.action

    [HttpGet]
    [Route("exportpowerpoint1")]
    public HttpResponseMessage Export()
    {           
        using (Presentation presentation = new Presentation(HttpContext.Current.Server.MapPath("~/PPTexports/testfile.pptx")))
        {
            MemoryStream stream = new MemoryStream();
            presentation.Save(stream, SaveFormat.Pptx);
            stream.Position = 0;
            var returnResult = Request.CreateResponse(HttpStatusCode.OK);
            returnResult.Content = new StreamContent(stream);
            returnResult.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.presentationml.presentation");
            returnResult.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "testfile.pptx"
            };                
            return returnResult;
        }}

with this code, I'm able to download the file but when I open the file then powerpoint gives this error message and the size of the file is also got double

Error message: powerpoint found unreadable content in testfile.pptx

I think the memory stream writes the file two times that is the reason size got double and file can't open because of duplicate content but I'm unable to find the cause of the issue can anybody help?

Ginish Sharma
  • 141
  • 1
  • 2
  • 9
  • what is Presentation ? refer to this: https://stackoverflow.com/questions/23768596/why-is-my-file-not-being-returned-by-a-get-request-from-my-web-api-function/23768883#23768883 – farbiondriven Nov 20 '19 at 07:47
  • I used aspose package for powerpoint so powerpoint class is in aspose NuGet package – Ginish Sharma Nov 20 '19 at 07:57
  • Is that `presentation.Save()` in the right place? You've not read anything into the stream at that point. Also should you not be returning a `IHttpActionResult` of `FileHttpActionResult`. Have you looked at the downloaded file in notepad for example? My guess is that its got a bunch of text in it from the html, if it was a proper `pptx` you should be able change to the extension to .zip and open it with winzip. – cjb110 Nov 20 '19 at 08:23
  • I opened the downloaded file using Notepad and the content of the downloaded file is looks changed as the original one so I don't know the reason for that – Ginish Sharma Nov 21 '19 at 07:23

2 Answers2

0

Try this:

[HttpGet]
[Route("exportpowerpoint1")]
public HttpResponseMessage Export()
{   
    var returnResult = Request.CreateResponse(HttpStatusCode.OK);
    returnResult.Content = new StreamContent(File.OpenRead(HttpContext.Current.Server.MapPath("~/PPTexports/testfile.pptx")));
    returnResult.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.presentationml.presentation");
    returnResult.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = "testfile.pptx"
    };                
    return returnResult;
}
farbiondriven
  • 2,450
  • 2
  • 15
  • 31
0

Don't do what I did and put the MemoryStream in a using block... you will get no response since it has been disposed before the content has been sent.

ccook
  • 5,869
  • 6
  • 56
  • 81