【问题标题】:Open a pdf file(using bytes) stored in Azure Blob storage打开存储在 Azure Blob 存储中的 pdf 文件(使用字节)
【发布时间】:2019-04-04 18:50:34
【问题描述】:

我正在尝试调整旧逻辑以支持来自 blob 的文件,谁能指导我如何打开存储在 azure blob 存储中的 pdf 文件。

我尝试搜索并找到了答案How to download a file to browser from Azure Blob Storage 这是使用 SAS 配置来做到这一点(如果我没记错的话)。

有什么办法可以转成字节吗?

从 windows 位置打开 pdf 文件的早期逻辑

Response.Buffer = true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "inline; filename=" + mapid + ".pdf");

FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] dataBytes = br.ReadBytes((int)(fs.Length - 1));
Response.BinaryWrite(dataBytes);
br.Close();
fs.Close();

我正在重写从 blob 读取文件的逻辑,下面的代码是我迄今为止尝试过的,

Byte[] dataBytes1;
CloudBlockBlob blobfile = GetStorageAccount(true).GetBlockBlobReference(filename);
blobfile.FetchAttributes();

using (StreamReader blobfilestream = new StreamReader(blobfile.OpenRead()))
{
    dataBytes1 = blobfilestream.CurrentEncoding.GetBytes(blobfilestream.ReadToEnd());
}
Byte[] value = BitConverter.GetBytes(dataBytes1.Length - 1);
Response.BinaryWrite(value);

但文件未打开,出现“加载失败”错误。 如果这是一个好方法,谁能指导我?

【问题讨论】:

  • 下面的答案对你有用吗?
  • 关于这个问题的任何更新? :)

标签: c# azure azure-blob-storage


【解决方案1】:

您可以使用DownloadToStreamAsync 并使用Response.OutputStream 作为目标流。

await blob.DownloadToStreamAsync(Response.OutputStream);

【讨论】:

    【解决方案2】:

    您可以使用DownloadToByteArray,示例代码(在 asp.net mvc 项目中)如下,在我身边可以正常工作:

            public ActionResult DownloadFile()
            {
                CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials("your_account", "your_key"), true);
                CloudBlobClient client = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer blobContainer = client.GetContainerReference("t11");
                CloudBlockBlob blob = blobContainer.GetBlockBlobReference("ss22.pdf");
    
                blob.FetchAttributes();
    
                long fileByteLength = blob.Properties.Length;
                byte[] fileContent = new byte[fileByteLength];
                for (int i=0;i<fileByteLength;i++)
                {
                    fileContent[i] = 0x20;
                }
    
                blob.DownloadToByteArray(fileContent,0);
    
                Response.BinaryWrite(fileContent);
    
                return new EmptyResult();
            }
    

    或者根据@hardkoded 提到的,您可以根据需要使用DownloadToStreamAsync 或DownloadToStream。

    示例代码如下(asp.net mvc项目):

            public ActionResult DownloadFile()
            {
                CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials("your_account", "your_key"),true);
                CloudBlobClient client = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer blobContainer = client.GetContainerReference("t11");
                CloudBlockBlob blob = blobContainer.GetBlockBlobReference("ss22.pdf");
    
                blob.DownloadToStream(Response.OutputStream);
    
                return new EmptyResult();
            }
    

    测试结果如下:

    【讨论】:

      【解决方案3】:

      如上所述,您需要使用 DownloadToStreamAsync。下面是我的代码。

              blobfile.FetchAttributes();
      
              using (StreamReader blobfilestream = new StreamReader(blobfile.OpenRead()))
              {
                  dataBytes1 = blobfilestream.CurrentEncoding.GetBytes(blobfilestream.ReadToEnd());
                  await blobfile.DownloadToStreamAsync(Response.OutputStream);
              }
      
              Byte[] value = BitConverter.GetBytes(dataBytes1.Length - 1);
              string mimeType = "application/pdf";
              Response.AppendHeader("Content-Disposition", "inline; filename="+ filename);
      
              return File(value, mimeType);
      

      【讨论】:

        【解决方案4】:
             public async System.Threading.Tasks.Task<ActionResult> DownloadFile()
            {
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                ConfigurationManager.ConnectionStrings["azureconnectionstring"].ConnectionString);
                CloudBlobClient client = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer blobContainer = client.GetContainerReference("my-blob-storage");
            CloudBlockBlob blob = blobContainer.GetBlockBlobReference("filename.pdf");
                var exists = blob.Exists(); // to verify if file exist
                blob.FetchAttributes();
                byte[] dataBytes1;
                using (StreamReader blobfilestream = new StreamReader(blob.OpenRead()))
                {
                    dataBytes1 = blobfilestream.CurrentEncoding.GetBytes(blobfilestream.ReadToEnd());
                    await blob.DownloadToStreamAsync(Response.OutputStream);
                }
        
                Byte[] value = BitConverter.GetBytes(dataBytes1.Length - 1);
                string mimeType = "application/pdf";
                Response.AppendHeader("Content-Disposition", "inline; filename=" + "filename.pdf");
        
                return File(value, mimeType);
        

        }

        【讨论】:

        • 虽然这可能回答了这个问题,但您应该edit 您的回答包括一些关于您的代码如何回答问题的解释,以便为未来的读者提供更多上下文。代码块本身不会立即对以后可能遇到相同问题的人有用。
        猜你喜欢
        • 2012-05-20
        • 1970-01-01
        • 1970-01-01
        • 2018-07-06
        • 2021-05-06
        • 2023-04-08
        • 1970-01-01
        • 2021-07-09
        • 2017-03-26
        相关资源
        最近更新 更多