我想出的解决这个问题的解决方案是将源流分成块并多次调用“PostFileWithRequest”。我在通话后更新进度条。使用这种方法很容易实现取消和重新启动上传,但我确信这不会特别有效。
无论如何,在伪代码中,我的解决方案看起来像这样:
using (var client = new JsonServiceClient(WebServiceAddress))
{
var src = GetStreamForFileToSend();
long totalBytes = src.CanSeek ? src.Length : 0;
long byteOffset = 0;
byte[] Chunk = new byte[Constants.UploadChunkSize];
for (int read; (read = src.Read(Chunk, 0, Chunk.Length)) != 0;)
{
// Progress update
UploadProgress(byteOffset, totalBytes);
using (var mem = new MemoryStream(Chunk, 0, read))
{
// The request contains a guid so the web service can concatenate chunks
// from the same client
var request = new UploadFileData(MyUniqueClientGuid, byteOffset, totalBytes);
// Send the chunk
UploadFileDataResponse response = client.PostFileWithRequest<UploadFileData>(
UploadFileData.Route, mem, filename, request);
// Cancelling supported easily...
if (response.Cancelled)
break;
byteOffset += read;
// Can also use 'src.Seek()' and send only the remainder of the file if the
// response contains info about how much of the file is already uploaded.
}
}
}