IE 11 无法从 Angular 中的字节数组创建文件对象 2

IE 11 failed to create file object from byte array in Angular 2

谁能告诉我为什么 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.

由于 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]);

文件是 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就是一个文件。

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

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