从ASP.net C#网站从Azure Blob存储下载文件时发生错误

时间:2018-12-20 21:30:35

标签: c# asp.net azure httpresponse

我试图将文件从Azure blob存储下载到我的网站,然后下载到发起下载的人员浏览器中,但有时会收到错误消息。到目前为止,我收到以下错误:

  1. 使用自定义TextWriter时,OutputStream不可用。
  2. 发送HTTP标头后,服务器无法附加标头。
  3. 发送HTTP标头后,服务器无法修改cookie。

我尝试过几件事,第一次尝试是这样:

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);

两者都随机抛出错误,但有时它们会起作用。

0 个答案:

没有答案