【发布时间】:2021-07-03 00:31:17
【问题描述】:
我正在尝试获取在客户端上创建的 zip 文件,并通过 Hub 流将该文件分块上传。当流结束时,我尝试打开文件,windows 状态它无法打开,{file name} 无效。
在执行 File.WriteAllBytes(path, bytes[]) 时,字节数组在分块之前在客户端上写得很好。查看流式文件和 File.WriteAllBytes 时,文件大小相同。
我没有想法......
客户端
private async IAsyncEnumerable<byte[]> StreamBytes(byte[] bytes)
{
//this works and I'm able to open the file it creates.
File.WriteAllBytes(@"C:\test.zip", bytes);
long fileSize = bytes.Length;
long fileWrite = 0;
while (fileSize != 0)
{
byte[] buffer = fileSize > ByteHelper.BUFFER_SIZE ? new byte[ByteHelper.BUFFER_SIZE] : new byte[fileSize];
Array.Copy(bytes, buffer, buffer.Length);
fileSize -= buffer.Length;
fileWrite += buffer.Length;
var bufResult = await Task.FromResult(buffer);
yield return bufResult;
}
}
服务器端
public async Task UploadCommandResultStream(IAsyncEnumerable<byte[]> byteChunk, int commandQueueKey, string folder)
{
var path = Path.Combine(_settings.UploadPath, folder);
if (Directory.Exists(path) == false)
{
Directory.CreateDirectory(path);
}
var file = Path.Combine(path, $"{commandQueueKey}.zip");
long bytesread = 0;
try
{
//this creates a file but can't open
using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read, ByteHelper.BUFFER_SIZE))
{
await foreach (var bytes in byteChunk)
{
fs.Write(bytes, 0, bytes.Length);
bytesread += bytes.Length;
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "asyncwrite");
}
}
【问题讨论】: