C# 如何返回 MemoryStream docx 文件 MVC?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14630336/
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
How do I return a MemoryStream docx file MVC?
提问by MrM
I have a docx file that I would like to return after I make edits. I have the following code...
我有一个 docx 文件,我想在编辑后返回。我有以下代码...
object useFile = Server.MapPath("~/Documents/File.docx");
object saveFile = Server.MapPath("~/Documents/savedFile.docx");
MemoryStream newDoc = repo.ChangeFile(useFile, saveFile);
return File(newDoc.GetBuffer().ToArray(), "application/docx", Server.UrlEncode("NewFile.docx"));
The file seems fine, but I am getting error messages ("the file being corrupt" and another stating "Word found unreadable content. If you trust the source click Yes"). Any ideas?
该文件看起来不错,但我收到错误消息(“文件已损坏”和另一条说明“Word 发现无法读取的内容。如果您信任来源,请单击是”)。有任何想法吗?
Thanks in advance
提前致谢
EDIT
编辑
This is the ChangeFile in my Model...
这是我模型中的 ChangeFile...
public MemoryStream ChangeFile(object useFile, object saveFile)
{
byte[] byteArray = File.ReadAllBytes(useFile.ToString());
using (MemoryStream ms = new MemoryStream())
{
ms.Write(byteArray, 0, (int)byteArray.Length);
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(ms, true))
{
string documentText;
using (StreamReader reader = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
documentText = reader.ReadToEnd();
}
documentText = documentText.Replace("##date##", DateTime.Today.ToShortDateString());
using (StreamWriter writer = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
writer.Write(documentText);
}
}
File.WriteAllBytes(saveFile.ToString(), ms.ToArray());
return ms;
}
}
回答by Parker
I use a FileStreamResult:
我使用FileStreamResult:
var cd = new System.Net.Mime.ContentDisposition
{
FileName = fileName,
// always prompt the user for downloading, set to true if you want
// the browser to try to show the file inline
Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return new FileStreamResult(documentStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
回答by Lloyd
Don't use MemoryStream.GetBuffer().ToArray()
use MemoryStream.ToArray()
.
不要使用MemoryStream.GetBuffer().ToArray()
use MemoryStream.ToArray()
。
The reason why is GetBuffer()
relates to the array used to create the memory stream and not the actual data in the memory stream. The underlaying array could actually differ in size.
原因GetBuffer()
与用于创建内存流的数组有关,而不是与内存流中的实际数据有关。底层数组的大小实际上可能不同。
Hidden on MSDN:
隐藏在 MSDN 上:
Note that the buffer contains allocated bytes which might be unused. For example, if the string "test" is written into the MemoryStream object, the length of the buffer returned from GetBuffer is 256, not 4, with 252 bytes unused. To obtain only the data in the buffer, use the ToArray method; however, ToArray creates a copy of the data in memory.
请注意,缓冲区包含可能未使用的已分配字节。例如,如果将字符串“test”写入 MemoryStream 对象,则 GetBuffer 返回的缓冲区长度为 256,而不是 4,其中 252 个字节未使用。要仅获取缓冲区中的数据,请使用 ToArray 方法;但是,ToArray 在内存中创建数据的副本。