Writing MemoryStream to Response Object

后端 未结 8 1513
闹比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: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();
    

提交回复
热议问题