Writing MemoryStream to Response Object

后端 未结 8 1516
闹比i
闹比i 2020-11-27 20:03

I am using the following code to stream pptx which is in a MemoryStream object but when I open it I get Repair message in PowerPoint, what is the correct way of writing Memo

8条回答
  •  难免孤独
    2020-11-27 20:43

    Assuming you can get a Stream, FileStream or MemoryStream for instance, you can do this:

    Stream file = [Some Code that Gets you a stream];
    var filename = [The name of the file you want to user to download/see];
    
    if (file != null && file.CanRead)
    {
        context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
        context.Response.ContentType = "application/octet-stream";
        context.Response.ClearContent();
        file.CopyTo(context.Response.OutputStream);
    }
    

    Thats a copy and paste from some of my working code, so the content type might not be what youre looking for, but writing the stream to the response is the trick on the last line.

提交回复
热议问题