我有一个WCF服务,允许我以块的形式上传文件。我想知道的是,这个代码是否会导致上传的流在任何情况下仅部分附加到目标流?
我的日志告诉我所有发送的流都是512000字节(我在客户端设置),到目前为止我已经从9个块中发送了6个块。但在服务器上,文件大小为2634325.这意味着发送的最后一个块(第6个)保存不完整。
导致此行为的原因是什么?我该怎么做才能避免这种情况?
或者这是否完全安全,我应该在其他地方寻找错误?
public void UploadChunk ( RemoteFileChunk file )
{
/// this file is not touched by anyone else
var uploadPath = @"C:\some path\some.file";
using ( var targetStream = new FileStream(uploadPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None) )
{
if ( targetStream.Length == file.ChunkNumber * Helper.ChunkSize )
{
/// ---- streaming operation is here ----
targetStream.Position = targetStream.Length;
file.Stream.CopyTo(targetStream);
file.Stream.Close();
/// -------------------------------------
}
else throw new FaultException<DataIntegrityException>(new DataIntegrityException
{
CurrentIndex = targetStream.Length,
RequestedIndex = file.ChunkNumber * Helper.ChunkSize,
Message = string.Format("{0}th chunk index requested when there already {1} chunks exist.", file.ChunkNumber, targetStream.Length / Helper.ChunkSize)
});
}
}
以下是客户端文件上传代码段:
var buffer = new byte[ChunkIndex != NumberOfChunks - 1 ? Helper.ChunkSize : LastPieceLen];
using ( var file = new FileStream(Info.FullName, FileMode.Open, FileAccess.Read, FileShare.None) )
{
file.Position = (long)ChunkIndex * Helper.ChunkSize;
file.Read(buffer, 0, buffer.Length);
}
var chunk = new MemoryStream(buffer);
chunk.Position = 0;
Service.StreamingEnd.UploadChunk(new RemoteFileChunk(FileId, CF.Id, VersionDate, ChunkIndex, chunk.Length, Sessions.Get(), chunk));
答案 0 :(得分:0)
看看if语句 - 最后的块是否保证恰好是块大小?如果您上传的文件大小不是块大小的倍数会怎样?