将文件名设置为 Blob 文件

2023-12-24

我想向我的 Blob 文件添加一个文件名,但我真的不知道该怎么做,这是我目前的代码:

  onClick() {
    var myHeader = new Headers();

    myHeader.append('Content-Type', 'text/plain');

    fetch(this.props.url, {
      method: 'POST',
      headers: myHeader,
      body: JSON.stringify(this.state.api_arg)
    }).then(response => {
      const filename = getFileName(response.headers.get('Content-Disposition'))

      response.blob().then(myBlob => {
        const fileUrl = URL.createObjectURL(myBlob)

        console.log(fileUrl)
        window.open(fileUrl)
      })
    })
 }

我的文件名存储在一个变量中。


Niels 的答案不完整,要处理 blob 中的文件名,您必须这样做:

const file = new File([myBlob], filename)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将文件名设置为 Blob 文件 的相关文章