我们使用Azure存储文件,当我们上载\下载文件时,它使用Azure模拟器,一切正常,并下载完整大小的文件。
但是,当我们尝试在使用Azure的舞台环境中下载文件时,发现了文件无法完全下载的问题。
下载代码如下:
CloudBlobContainer blobContainer = await GetContainerAsync(tenantId);
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileId);
return await blockBlob.DownloadRangeToByteArrayAsync(target, index, offset, length);
根据API,我们执行以下操作:
var read = await _fileStorageService.DownloadRangeToByteArrayAsync(id, buffer, 0, offset, dataToRead > bufferSize ? bufferSize : dataToRead);
offset += read;
dataToRead -= read;
await Response.Body.WriteAsync(buffer, 0, read);
await Response.Body.FlushAsync();
我们在这里做错了什么?预先感谢
答案 0 :(得分:0)
我认为您将错误的长度传递给方法,并且仅下载该长度的文件。如果要下载整个文件,则应执行以下操作:
CloudBlobContainer blobContainer = await GetContainerAsync(tenantId);
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileId);
return await blockBlob.DownloadToByteArrayAsync(target, 0);
答案 1 :(得分:0)
尝试这样。您需要在blob上执行FetchAttributesAsync
,以获取字节数组的初始大小。如果要分块下载,只需使用不同的索引执行多个DownloadToByteArrayAsync
调用。
public async Task<byte[]> GetFile(string container, string directory, string blobName)
{
CloudBlobContainer cloudContainer = client.GetContainerReference(container);
CloudBlobDirectory cloudDirectory = cloudContainer.GetDirectoryReference(directory);
CloudBlockBlob blob = cloudDirectory.GetBlockBlobReference(blobName);
await blob.FetchAttributesAsync();
long fileByteLength = blob.Properties.Length;
byte[] fileContent = new byte[fileByteLength];
for (int i = 0; i < fileByteLength; i++)
{
fileContent[i] = 0x20;
}
await blob.DownloadToByteArrayAsync(fileContent, 0);
return fileContent;
}