WebAPI使用内存流下载PowerPoint文件

时间:2018-02-26 15:47:04

标签: c# asp.net-web-api2 syncfusion

我在使用Powerpoint演示文稿库(Syncfusion)下载包含内容的文件时遇到问题。文档只提供ASP示例,没有专门的Web API。

我可以将文件保存到文件系统中,该文件系统具有我添加到Powerpoint的上下文 我可以使用API​​将文件下载到用户浏览器,但此Powerpoint为空。

Syncfusion具有将Powerpoint保存到内存流的功能,所以我想我的问题是使用流中的内容将文件保存到用户浏览器的正确方法是什么?

我正在使用HTTPGet并通过浏览器点击链接。
我是否需要发送上下文类型或类似内容?

感谢您的帮助,如果有帮助,我可以提供迄今为止的帮助。 裴家

修改

    [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;
    }

这就是我所拥有的,库将表示保存到内存流中,您可以将参数更改为将其写入文件的字符串,并且可以选择为MVC传递文件名,格式和HttpResponse但是我无法使用我的API控制器。我一直收到网络错误,但不知道为什么。

再次感谢

1 个答案:

答案 0 :(得分: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. :-)