简体   繁体   English

使用MemoryStream在远程位置C#上写入.docx文件

[英]Using MemoryStream to write .docx file on remote location c#

I am generating MemoryStream from string and pass it to another method which will save that Stream at remote loaction in form of .docx file. 我正在从字符串生成MemoryStream并将其传递给另一个方法,该方法将以.docx文件的形式将该Stream保存在远程位置。

public static MemoryStream GenerateStreamFromString(string s)
{
    MemoryStream stream = new MemoryStream();
    StreamWriter writer = new StreamWriter(stream);
    writer.Write(s);
    writer.Flush();
    stream.Position = 0;
    return stream;
}

Above code is for convert the string to MemoryStream. 上面的代码用于将字符串转换为MemoryStream。 The string I am passing to the above method is XML which I get by Globals.ThisAddIn.Application.ActiveDocument.WordOpenXML line. 我传递给上述方法的字符串是XML,它是通过Globals.ThisAddIn.Application.ActiveDocument.WordOpenXML行获得的。

File successfully get uploaded to remote location but as I download that file, MS Word throws an error that File is corrupted. Either part of file is missing or invalid 文件已成功上传到远程位置,但是当我下载该文件时,MS Word抛出一个错误,指出File is corrupted. Either part of file is missing or invalid File is corrupted. Either part of file is missing or invalid (As shown in below image) File is corrupted. Either part of file is missing or invalid (如下图所示)

在此处输入图片说明

I am not able to get that whether the Stream I am generating is wrong/corrupted or I need to perform some more operation to make this .docx file valid? 我无法得知我正在生成的流是错误的/损坏了,还是需要执行更多操作才能使此.docx文件有效?

Sorry for this question, there are so many questions like this, but none could be more helpful. 对不起,这个问题有很多这样的问题,但是没有一个会更有用。

The XML passed this way is not a valid DOCX file. 通过这种方式传递的XML无效的DOCX文件。 The actual DOCX format uses compressed content with folders. 实际的DOCX格式使用带有文件夹的压缩内容。 You need to save the document from your add in to a temporary location (best use GetTempPath , then read the file from disk into your memory stream and upload it wherever you need. 您需要将文档从添加项保存到一个临时位置(最好使用GetTempPath ,然后将文件从磁盘读取到内存流中,然后将其上传到任何需要的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM