如何从StreamWriter获取byte[]
并使用写入Streamwriter的内容下载文件?
string document = @"C:\TestArea\Destination\SUP000011\ATM-1B4L2KQ0ZE0-0001\Sample.docx";
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
Regex regexText = new Regex("Hello world!");
docText = regexText.Replace(docText, "Hi Everyone!");
using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
}
下载代码段-
byte[] bytearraydata = Encoding.ASCII.GetBytes(doctext);
if (bytearraydata != null)
{
Response.ContentType = "Application/msword";
Response.AppendHeader("Content-disposition",
"attachment; filename=" + "Sample.docx");
Response.AppendHeader("content-length", bytearraydata.Length.ToString());
Response.BinaryWrite(bytearraydata);
Response.End();
}
答案 0 :(得分:0)
尽管名称StreamWriter
实际上是写字符,而不是字节。如果要写入未编码的字节,请使用BinaryWriter
。
要读取未编码的字节,请使用BinaryReader
。