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

时间:2017-06-05 20:50:08

标签: angular internet-explorer-11

有人可以告诉我为什么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.

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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是一个文件。