我试图将文件从Azure blob存储下载到我的网站,然后下载到发起下载的人员浏览器中,但有时会收到错误消息。到目前为止,我收到以下错误:
我尝试过几件事,第一次尝试是这样:
var account = CloudStorageAccount.Parse(Properties.Settings.Default.AzureConnectionString);
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(Properties.Settings.Default.AzureContainer);
var blob = container.GetBlobReference(path);
if (blob.Exists())
{
MemoryStream memStream = new MemoryStream();
blob.DownloadToStream(memStream);
httpCurrentContext.Response.ContentType = mimeType;
httpCurrentContext.Response.AddHeader("Content-Disposition", "Attachment; filename=" + name.ToString());
httpCurrentContext.Response.AddHeader("Content-Length", blob.Properties.Length.ToString());
httpCurrentContext.Response.BinaryWrite(memStream.ToArray());
httpCurrentContext.Response.Flush();
httpCurrentContext.Response.End();
}
第二次尝试是用以下代码替换BinaryWrite:
httpCurrentContext.Response.BufferOutput = true;
httpCurrentContext.Response.OutputStream.Write(memStream.ToArray(), 0, (int)blob.Properties.Length);
两者都随机抛出错误,但有时它们会起作用。