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

让人想犯罪 __ 提交于 2020-02-29 17:04:45

问题


Can anybody tell me why IE11 is throwing error at the last line -

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 });

This is working in both Chrome and Firefox.IE throws object error -

Object doesn't support this action.

回答1:


Files are Blobs plus meta properties, so you can just add the necessary properties like this :

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

Then the blob is a file.




回答2:


As IE does not support constructor of File API, I have come up with the following workaround. Hope this helps to others in future -

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


来源:https://stackoverflow.com/questions/44377464/ie-11-failed-to-create-file-object-from-byte-array-in-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!