【问题标题】:IE 11 failed to create file object from byte array in Angular 2IE 11 无法从 Angular 2 中的字节数组创建文件对象
【发布时间】:2021-10-05 04:39:29
【问题描述】:

谁能告诉我为什么 IE11 在最后一行抛出错误 -

this.document = this.control.value;
  const bytes = 
this.documentService.base64toBytes(this.document.documentBlob.documentData, 
       this.document.documentDataFormat);
const file = new File(bytes, this.document.documentName, { type: 
       this.document.documentDataFormat });

这在 Chrome 和 Firefox 中都有效。IE 抛出对象错误 -

Object doesn't support this action.

【问题讨论】:

  • IE11 似乎不支持 caniuse.com/#search=file 的文件构造函数
  • 糟糕!那么作为解决方法的建议是什么??

标签: angular internet-explorer-11


【解决方案1】:

文件是 Blob 加上元属性,因此您可以像这样添加必要的属性:

let blob = this.documentService.base64toBytes(this.document.documentBlob.documentData, this.document.documentDataFormat);
// and add the meta properties
blob['lastModifiedDate'] = new Date();
blob['name'] = 'fileName';

那么blob就是一个文件。

【讨论】:

  • 哇。这真的很容易。经过 20 分钟的疯狂谷歌搜索,在不使用 File 构造函数的情况下将我的 blob 转换为文件真的很容易。谢谢!
  • @SeanKPS 欢迎您,我很高兴能提供帮助!
  • 谢谢你!你的救星!!我喜欢这个解决方案而不是标记为答案的那个。对此竖起大拇指:)
  • @ajgo 很高兴它对你有所帮助;)
【解决方案2】:

由于 IE 不支持 File API 的构造函数,我想出了以下解决方法。希望这对将来的其他人有所帮助 -

const bytes = this.documentService.base64toBytes(this.document.documentBlob.documentData, this.document.documentDataFormat);
let file: File;
try {
  file = new File(bytes, this.document.documentName, { type: this.document.documentDataFormat });

  if (this.uploader.isFile(file)) {
    this.uploader.addToQueue([file]);
  }
} catch (err) { // Workaround for IE 11
  const blob = this.documentService.base64ToBlob(this.document.documentBlob.documentData,
    this.document.documentDataFormat);
  file = this.documentService.blobToFile(blob, this.document.documentName);
  this.uploader.addToQueue([file]);

【讨论】:

  • 保持 ie11 的缩写,而不是 "file = new File(bytes, this.document.documentName, { type: this.document.documentDataFormat });"你应该做“new Blob(bytes, { type: this.document.documentDataFormat })”
  • documentService.blobToFile 是什么样的?
  • @Brahim-LAMJAGUAR 的回答为我做了。
【解决方案3】:

这个解决方案对我来说是创建文件而不是使用 file()

let file = <File>Object.assign(new Blob([bytes]), { name: fileName });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-16
    • 2018-01-18
    • 2019-07-26
    • 1970-01-01
    • 2012-11-30
    • 2022-07-17
    • 2011-04-11
    • 1970-01-01
    相关资源
    最近更新 更多