Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.
like image 727
Mayeed Avatar asked Jun 05 '17 20:06

Mayeed


2 Answers

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.

like image 85
Brahim LAMJAGUAR Avatar answered Nov 20 '22 00:11

Brahim LAMJAGUAR


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]);
like image 30
Mayeed Avatar answered Nov 20 '22 00:11

Mayeed