Writing MemoryStream to Response Object

后端 未结 8 1449
闹比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:25

    I tried all variants of end, close, flush, and System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest() and none of them worked.

    Then I added the content length to the header: Response.AddHeader("Content-Length", asset.File_Size.ToString());

    In this example asset is a class that has a Int32 called File_Size

    This worked for me and nothing else did.

    0 讨论(0)
  • 2020-11-27 20:29

    Instead of creating the PowerPoint presentation in a MemoryStream write it directly to the Response.OutputStream. This way you don't need to be wasting any memory on the sever as the component will be directly streaming the output to the network socket stream. So instead of passing a MemoryStream to the function that is generating this presentation simply pass the Response.OutputStream.

    0 讨论(0)
  • 2020-11-27 20:37

    The problem for me was that my stream was not set to the origin before download.

    Response.Clear();
    Response.ContentType = "Application/msword";
    Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");
    
    //ADDED THIS LINE
    myMemoryStream.Seek(0,SeekOrigin.Begin);
    
    myMemoryStream.WriteTo(Response.OutputStream); 
    Response.Flush();
    Response.Close();
    
    0 讨论(0)
  • 2020-11-27 20:40

    I had the same issue. try this: copy to MemoryStream -> delete file -> download.

    string absolutePath = "~/your path";
    try {
        //copy to MemoryStream
        MemoryStream ms = new MemoryStream();
        using (FileStream fs = File.OpenRead(Server.MapPath(absolutePath))) 
        { 
            fs.CopyTo(ms); 
        }
    
        //Delete file
        if(File.Exists(Server.MapPath(absolutePath)))
           File.Delete(Server.MapPath(absolutePath))
    
        //Download file
        Response.Clear()
        Response.ContentType = "image/jpg";
        Response.AddHeader("Content-Disposition", "attachment;filename=\"" + absolutePath + "\"");
        Response.BinaryWrite(ms.ToArray())
    }
    catch {}
    
    Response.End();
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 20:43

    Try with this

    Response.Clear();
    Response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
    Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.pptx;", getLegalFileName(CurrentPresentation.Presentation_NM)));
    Response.Flush();                
    Response.BinaryWrite(masterPresentation.ToArray());
    Response.End();
    
    0 讨论(0)
提交回复
热议问题