使用FileStream复制Word文档会破坏doc

时间:2013-06-28 19:27:13

标签: c# wcf ms-word

我正在编写一个使用字节缓冲区传输大文件的WCF服务。我遇到Word docx的问题。我可以复制该文件,但是当我打开它时,我收到“文件损坏”错误,然后它询问我是否要打捞它。如果我说“是”,它就可以挽救它。

如何复制word doc?我已经尝试改变编码和我在网上找到的所有内容,没有任何帮助。

这是我的客户:

FileInfo SourceFile = new FileInfo(textBoxPath.Text);
LargeFileContractClient localClient = new LargeFileContractClient();

LargeFileInfo LocalLargeFileInfo = new LargeFileInfo();
LocalLargeFileInfo.TransactionID = Guid.NewGuid();
LocalLargeFileInfo.FileName = SourceFile.Name;

LargeFileInfo localLargeFileResponse = new LargeFileInfo();

using (BinaryReader SourceStream = new BinaryReader(File.Open(SourceFile.FullName, FileMode.Open), Encoding.Unicode))
{
   while (SourceStream.BaseStream.Position < SourceStream.BaseStream.Length)
   {
       //Get a set of bytes and pack them into the largefileinfo
       int BytesRead = SourceStream.Read(FileBuffer, 0, 1000);

       LocalLargeFileInfo.FileContent = FileBuffer;

       //Send the LargeFileInfo to the server
       localLargeFileResponse = localClient.TransferLargeFile(LocalLargeFileInfo);

   }
}

...和我的服务器代码:

public LargeFileInfo TransferLargeFile(LargeFileInfo argFileInfo)
{
   FileInfo DestFile = new FileInfo(String.Format(@"C:\LargeFiles\{0}\{1}", argFileInfo.TransactionID, argFileInfo.FileName));

   if(!DestFile.Directory.Exists)
   {
       DestFile.Directory.Create();
   }

   using (BinaryWriter DestStream = new BinaryWriter(new FileStream (DestFile.FullName, FileMode.Append), Encoding.Unicode))
   {
       DestStream.Write(argFileInfo.FileContent, 0, argFileInfo.FileContent.Length);
   }

   argFileInfo.ContinueFrom += argFileInfo.FileContent.Length;

   return argFileInfo;
}

我尝试过更改编码。我尝试过使用普通文件流。我已经尝试了所有我能想到的东西,而且我无法在最后打开文件。

我可以复制拉链和AutoCAD dwg和PDF但不能复制Word文档。他们有什么特别之处?

0 个答案:

没有答案
相关问题