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

May*_*eed 7 internet-explorer-11 angular

谁能告诉我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 });
Run Code Online (Sandbox Code Playgroud)

这在Chrome和Firefox中都可以使用。IE抛出对象错误-

Object doesn't support this action.
Run Code Online (Sandbox Code Playgroud)

Bra*_*UAR 9

文件是 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';
Run Code Online (Sandbox Code Playgroud)

那么 blob 是一个文件。


May*_*eed 3

由于 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]);
Run Code Online (Sandbox Code Playgroud)