简体   繁体   English

试图从 azure blob 下载 word 文档

[英]trying to download the word document from azure blob

i am trying to download the word document stored in azure blob container having private access and i need to convert downloaded document into byte array so that i can be able to send to react app我正在尝试下载存储在 azure 具有私有访问权限的 blob 容器中的 word 文档,我需要将下载的文档转换为字节数组,以便我可以发送到响应应用程序

this is the code i am trying below这是我在下面尝试的代码

    [Authorize, HttpGet("{id}/{projectphase?}")]
    public async Task<ActionResult<DesignProject>> GetDesignProject(string id, string projectphase = null)
    {
          var blobContainerName = Startup.Configuration["AzureStorage:BlobContainerName"];
          var azureStorageConnectionString = Startup.Configuration["AzureStorage:ConnectionString"];

          BlobContainerClient blobContainerClient = new BlobContainerClient(azureStorageConnectionString, blobContainerName);
          blobContainerClient.CreateIfNotExists();
          .......  // not sure how to proceed further 
          .......
          ......
          return new InlineFileContentResult('here i need to return byte array???', "application/docx") { FileDownloadName = fileName };
    }

I have got the full path name where the file has been stored like as below我已经获得了存储文件的完整路径名,如下所示

https://xxxx.blob.core.windows.net/design-project-files/99999-99/99999-99-BOD-Concept.docx

and then i have got the file name as well 99999-99-BOD-Concept.docx然后我也得到了文件名99999-99-BOD-Concept.docx

Could any one please guide me how to proceed with the next to download the document that would be very grateful to me.谁能指导我如何进行下一步下载文件,将非常感谢我。

Please try something like the following (untested code though):请尝试以下类似的方法(尽管是未经测试的代码):

public async Task<ActionResult<DesignProject>> GetDesignProject(string id, string projectphase = null)
{
      var blobContainerName = Startup.Configuration["AzureStorage:BlobContainerName"];
      var azureStorageConnectionString = Startup.Configuration["AzureStorage:ConnectionString"];

      BlobContainerClient blobContainerClient = new BlobContainerClient(azureStorageConnectionString, blobContainerName);
      blobContainerClient.CreateIfNotExists();
      var blobClient = new BlobClient("https://xxxx.blob.core.windows.net/design-project-files/99999-99/99999-99-BOD-Concept.docx");
      var blobName = blobClient.Name;
      blobClient = new BlobClient(azureStorageConnectionString, blobContainerName, blobName);
      using (var ms = new MemoryStream())
      {
        await blobClient.DownloadToAsync(ms);
        return new InlineFileContentResult(ms.ToArray(), "application/docx") { FileDownloadName = fileName };
      }
}

Basically what we're doing is that we're first creating a BlobClient using the URL that you have so that we can extract blob's name out of that URL (you can do URL parsing as well).基本上我们正在做的是,我们首先使用您拥有的 URL 创建一个BlobClient ,以便我们可以从 URL 中提取 blob 的名称(您也可以像 ZE6B391A3D2C4D485703D 那样做) Once we have the blob's name, we create a new instance of BlobClient using connection string, blob container name and blob's name.一旦我们有了 blob 的名称,我们就可以使用连接字符串、blob 容器名称和 blob 的名称创建一个新的 BlobClient 实例。

Then we download the blob's content as stream and convert that stream to byte array (this part I am not 100% sure that my code would work) and return that byte array.然后我们将 blob 的内容下载为 stream 并将 stream 转换为字节数组(这部分我不是 100% 确定我的代码会工作)并返回该字节数组。

You don't really need to have this process where your react app requests to your server, so your server downloads the file and then sends it to the react app;你真的不需要在你的 react 应用程序向你的服务器请求这个过程,所以你的服务器会下载文件然后将它发送到 react 应用程序; that file in blob storage is on the web, downloadable from blob storage so it's kinda unnecessary to hassle your sevrer into being a proxy for it Blob 存储中的该文件位于 web 上,可从 Blob 存储下载,因此无需麻烦您的服务器成为它的代理

If you configure public access for blobs then you just put that URL into your react app - user clicks it, bytes download.如果您为 blob 配置公共访问权限,那么您只需将 URL 放入您的反应应用程序 - 用户单击它,字节下载。 Happy days.快乐的时光。 If you have a private container you can still generate SAS URLs for the blobs如果你有一个私有容器,你仍然可以为 blob 生成 SAS URL

If you actually need the bytes in your react app, then just fetch it with a javascript web request - you'll need to set a CORS policy on the blob container though如果您确实需要 React 应用程序中的字节,那么只需使用 javascript web 请求来获取它 - 您需要在 blob 容器上设置 CORS 策略


If you really want to download the file to/via the server, you'll probably have to get into streaming it to the response stream connected to the react app, passed into the SOMETHING below:如果您真的想将文件下载到/通过服务器,您可能必须将其流式传输到连接到 react 应用程序的响应 stream 中,并传递到下面的 SOMETHING 中:

        BlobClient blob = blobContainerClient.GetBlobClient( BLOB NAME I.E PATH INSIDE CONTAINER);

        //download to a file or stream
        await blob.DownloadToAsync( SOMETHING );

    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM