0

到目前为止,这是我下载 powerpoint 文件的代码。我将 aspose 包用于 powerpoint,这是 aspose 文档的链接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;
        }}

使用此代码,我可以下载文件,但是当我打开文件时,powerpoint 会给出此错误消息,并且文件的大小也会加倍

错误信息:powerpoint 在 testfile.pptx 中发现不可读的内容

我认为内存流两次写入文件,这是大小加倍并且文件由于重复内容而无法打开的原因,但我无法找到问题的原因,有人可以帮忙吗?

4

2 回答 2

0

尝试这个:

[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;
}
于 2019-11-20T08:15:31.427 回答
0

不要做我所做的并将 MemoryStream 放在 using 块中......您将不会收到任何响应,因为它在发送内容之前已被处置。

于 2020-05-11T17:17:30.613 回答