1

I'm having trouble downloading a file with content using a Powerpoint presentation library (Syncfusion). The docs only supply ASP examples, no Web API specifically.

I can save a file to the file system which has the context I add to the Powerpoint.
I can get the API to download a file to the users browser but this Powerpoint is empty.

Syncfusion has a function to save the Powerpoint to a memory stream so I guess my question is what is the correct way to save a file to the users browser with the content from the stream?

I'm using HTTPGet and hitting the link through the browser.
Do I need to sent the context-type or anything like that?

Thanks for your help,
I can provide what I have so far if that helps. Kurtis

Edit:

    [HttpGet, Route("")]
    public HttpResponseMessage Get()
    {
        var presentation = Presentation.Create();

        var firstSlide = presentation.Slides.Add(SlideLayoutType.Blank);
        var textShape = firstSlide.AddTextBox(100, 75, 756, 200);

        var paragraph = textShape.TextBody.AddParagraph();
        paragraph.HorizontalAlignment = HorizontalAlignmentType.Center;

        var textPart = paragraph.AddTextPart("Kurtis' Presentation");
        textPart.Font.FontSize = 80;

        var memoryStream = new MemoryStream();
        presentation.Save(memoryStream);

        var result = Request.CreateResponse(HttpStatusCode.OK);
        result.Content = new StreamContent(memoryStream);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = "export.pptx"
        };

        return result;
    }

This is what I have, the library saves the presentation to the memory stream, you can change the parameter to a string which writes this to a file and there is an option to pass a filename, format and HttpResponse for MVC but I couldn't get this working with my API controller. I kept getting a network error but didn't know why.

Thanks again

Kurtis
  • 1,172
  • 2
  • 12
  • 20
  • Open up a new stream to a file and write the contents of the web stream to there – Neijwiert Feb 26 '18 at 15:49
  • But that would download to the file system on the server wouldn't it? Or can I return the file in the request? If yes, can you provide an example? Thanks – Kurtis Feb 26 '18 at 15:50
  • Well to be fair you aren't really clear on what you are asking. To me it sounded as if you just wanted to save it to disk – Neijwiert Feb 26 '18 at 15:52
  • I put my question in bold and said that I can write the file to the file system. I need the file to download in the users browser. Any ideas? – Kurtis Feb 26 '18 at 15:53
  • So you have a WebAPI Action method, and in that method you have a `MemoryStream` of data, and you want to send that to the caller of your WebAPI method as a file download? In MVC you'd use a `FileStreamResult` or similar. Not sure what the WebAPI equivalent would be. Can you post your Action code so far? – Trevor Feb 26 '18 at 16:19
  • @Trevor thats exactly what I need. I have updated my question with my code. Thanks – Kurtis Feb 26 '18 at 16:49
  • @KurtisHardy why are you using a ApiController instead of a Controller for the FileContentResult Get() call? What is consuming your API? If it is just a button on a customer facing web page then your life is easier to move it to a Controller and use `public FileContentResult Get()` – tawman Feb 26 '18 at 16:58
  • 1
    Does this question help https://stackoverflow.com/q/12145390/8915494 ? It references a `StreamContent` [MSDN](https://msdn.microsoft.com/en-us/library/system.net.http.streamcontent(v=vs.118).aspx) class. – Trevor Feb 26 '18 at 16:58
  • @tawman because I don't or shouldn't need to reference MVC just for this one action. Web API can do it, I just don't know how. – Kurtis Feb 26 '18 at 17:20
  • @Trevor yes that worked! Why does using `new HttpResponseMessage` work but `Request.CreateResponse(...)` doesn't? Any ideas? And thanks for your help. Write an answer and I'll accept :-) – Kurtis Feb 26 '18 at 17:21
  • Ahh, actually, the position is the vitel part! Resetting the position to `0` is the key. `memoryStream.Position = 0;`. – Kurtis Feb 26 '18 at 17:23
  • 1
    Ah, of course. Sorry, I should have spotted that one. You should probably write up your own answer and accept it if that was the actual problem. :P – Trevor Feb 26 '18 at 17:24
  • OK. But thank you for the help, I would have never have gotten that. – Kurtis Feb 26 '18 at 17:32

1 Answers1

0

I found my problem with the help of others in the comments.

The code above work but I needed to reset the stream position to zero to actually write the data. memoryStream.Position = 0;

Thanks for those who commented. :-)

Kurtis
  • 1,172
  • 2
  • 12
  • 20