将MemoryStream写入响应对象

时间:2012-12-08 15:59:41

标签: c# asp.net httpresponse

我正在使用以下代码来流式传输一个MemoryStream对象中的pptx,但是当我打开它时,我在PowerPoint中获得了修复消息,将MemoryStream写入响应对象的正确方法是什么?

HttpResponse response = HttpContext.Current.Response;
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.BinaryWrite(masterPresentation.ToArray());
response.End();

8 个答案:

答案 0 :(得分:64)

我遇到了同样的问题,唯一有效的解决方案是:

Response.Clear();
Response.ContentType = "Application/msword";
Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");
Response.BinaryWrite(myMemoryStream.ToArray());
// myMemoryStream.WriteTo(Response.OutputStream); //works too
Response.Flush();
Response.Close();
Response.End();

答案 1 :(得分:11)

假设您可以获得Stream,FileStream或MemoryStream,您可以这样做:

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

这是我的一些工作代码的复制和粘贴,因此内容类型可能不是您要查找的内容,但将流写入响应是最后一行的技巧。

答案 2 :(得分:7)

不是在MemoryStream中创建PowerPoint演示文稿,而是直接将其写入Response.OutputStream。这样您就不需要在服务器上浪费任何内存,因为组件将直接将输出流式传输到网络套接字流。因此,不要将MemoryStream传递给生成此演示文稿的函数,而只需传递Response.OutputStream。

答案 3 :(得分:4)

试试这个

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();

答案 4 :(得分:3)

首先我们需要写入我们的内存流,然后借助内存流方法“WriteTo”,我们可以写入页面的响应,如下面的代码所示。

   MemoryStream filecontent = null;
   filecontent =//CommonUtility.ExportToPdf(inputXMLtoXSLT);(This will be your MemeoryStream Content)
   Response.ContentType = "image/pdf";
   string headerValue = string.Format("attachment; filename={0}", formName.ToUpper() + ".pdf");
   Response.AppendHeader("Content-Disposition", headerValue);

   filecontent.WriteTo(Response.OutputStream);

   Response.End();

FormName是给定的fileName,此代码将通过调用PopUp使生成的PDF文件可下载。

答案 5 :(得分:1)

我尝试了end,close,flush和System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest()的所有变体,但没有一个工作。

然后我将内容长度添加到标题中: Response.AddHeader(" Content-Length",asset.File_Size.ToString());

在此示例中,asset是一个具有Int32的类,名为File_Size

这对我有用,没有别的。

答案 6 :(得分:0)

我有同样的问题。 试试这个:复制到MemoryStream - >删除文件 - >下载。

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();

答案 7 :(得分:0)

对我来说,问题是我的流没有在下载前设置为原始。

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();