C# 将 MemoryStream 写入响应对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13779139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing MemoryStream to Response Object
提问by Ali
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 MemoryStream to Response Object?
我正在使用以下代码来流式传输 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();
采纳答案by playful
I had the same problem and the only solution that worked was:
我遇到了同样的问题,唯一有效的解决方案是:
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();
回答by Darin Dimitrov
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.
不是在 MemoryStream 中创建 PowerPoint 演示文稿,而是将其直接写入Response.OutputStream. 这样您就不需要在服务器上浪费任何内存,因为组件将直接将输出流式传输到网络套接字流。因此,不要将 MemoryStream 传递给生成此演示文稿的函数,只需传递 Response.OutputStream。
回答by Nudier Mena
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();
回答by Ian Robertson
Assuming you can get a Stream, FileStream or MemoryStream for instance, you can do this:
假设您可以获得 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);
}
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.
这是我的一些工作代码的复制和粘贴,因此内容类型可能不是您要查找的内容,但是将流写入响应是最后一行的技巧。
回答by Deepak Kothari
First We Need To Write into our Memory Stream and then with the help of Memory Stream method "WriteTo" we can write to the Response of the Page as shown in the below code.
首先我们需要写入我们的内存流,然后在内存流方法“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 is the fileName given,This code will make the generated PDF file downloadable by invoking a PopUp.
FormName 是给定的文件名,此代码将通过调用 PopUp 使生成的 PDF 文件可下载。
回答by Norwood
I tried all variants of end, close, flush, and System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest() and none of them worked.
我尝试了 end、close、flush 和 System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest() 的所有变体,但没有一个起作用。
Then I added the content length to the header: Response.AddHeader("Content-Length", asset.File_Size.ToString());
然后我将内容长度添加到标题中: Response.AddHeader("Content-Length", asset.File_Size.ToString());
In this example asset is a class that has a Int32 called File_Size
在这个例子中,资产是一个类,它有一个名为 File_Size 的 Int32
This worked for me and nothing else did.
这对我有用,没有其他作用。
回答by Alexander SS
I had the same issue. try this: copy to MemoryStream -> delete file -> download.
我遇到过同样的问题。试试这个:复制到 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();
回答by Ryan Bennett
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();

