【问题标题】:trying to download a file from an azure file share via react app尝试通过 React 应用程序从 azure 文件共享中下载文件
【发布时间】:2020-07-30 08:02:56
【问题描述】:

我正在尝试通过 react 应用从 azure 文件共享中下载文件 我很好地连接了一个文件客户端并使用this method下载它

文档的方式并不多,所以我试图导航承诺以获取文件内容以下载它们using this

我返回的对象在控制台日志记录的下方。

{
  "lastModified": "2020-04-09T21:01:45.000Z",
  "metadata": {},
  "contentType": "application/x-zip-compressed",
  "requestId": "xxx-401a-004e-193c-xxx",
  "version": "2019-07-07",
  "isServerEncrypted": true,
  "fileAttributes": "Archive",
  "fileCreatedOn": "2020-04-09T21:01:45.148Z",
  "fileLastWriteOn": "2020-04-09T21:01:45.148Z",
  "fileChangeOn": "2020-04-09T21:01:45.148Z",
  "filePermissionKey": "xxx*xxx",
  "fileId": "xxxxx",
  "fileParentId": "xxxxx",
  "leaseState": "available",
  "leaseStatus": "unlocked",
  "blobBody": {}
}

...

blobBody: Promise { "fulfilled" }
​​
<state>: "fulfilled"
​​
<value>: Blob
​​​
size: 1960118
​​​
type: "application/x-zip-compressed"
​​​
<prototype>: BlobPrototype
​​​​
arrayBuffer: function arrayBuffer()
​​​​
constructor: function ()
​​​​
size: 
​​​​
slice: function slice()
​​​​
stream: function stream()
​​​​
text: function text()

​​​​ 我尝试调用 stream 或 arrayBuffer 函数,但我似乎无法访问 promise 内的任何内容

console.log(`downloading file: ${fileName}`)
const fileClient = this.state.doneDirClient.getFileClient(fileName)
const file = await fileClient.download()
console.log(file)
console.log(file.blobBody.Blob)

最后一行返回undefined

使用有效的修改代码进行编辑:

  async download(fileName: string) {
    const fileClient = this.state.doneDirClient.getFileClient(fileName)
    const file = await fileClient.download()
    Promise.resolve(file.blobBody).then(function (value) {
      fileDownload(value, fileName)
    });
  }

如何获取文件内容?

【问题讨论】:

  • 根据文档file.blobBody 是一个承诺。您是否尝试过兑现承诺?
  • @GauravMantri 成功了,谢谢。如果您想发布解决方案,很乐意回来查看并给您信用

标签: javascript reactjs azure azure-storage azure-storage-files


【解决方案1】:

如果您查看FileDownloadResponse 的定义,您会注意到blobBody 参数本质上是一个Promise

type FileDownloadResponse = FileDownloadHeaders & { _response: Object, blobBody: Promise<Blob>, readableStreamBody: NodeJS.ReadableStream }

一旦你兑现了这个承诺,你应该得到Blob

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 2017-10-06
    • 2022-01-22
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    相关资源
    最近更新 更多