11

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.
2
  • 2
    IE11 does not appear to support the file constructor per caniuse.com/#search=file
    – silentsod
    Jun 5, 2017 at 21:40
  • Oops!! What do suggest then as workaround??
    – Mayeed
    Jun 6, 2017 at 0:48

3 Answers 3

9

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.

4
  • 1
    wow. It's really that easy. After 20 minutes of frantic googling, turning my blob into a file without using a File constructor is really that easy. Thanks!
    – SeanMC
    Jan 31, 2019 at 20:19
  • 1
    @SeanKPS you are welcome, I'm glad I was able to help! Feb 1, 2019 at 13:46
  • 1
    thanks man! your a lifesaver!! I like this solution than the one marked as answer. thumbs up for this :)
    – aj go
    Sep 26, 2019 at 13:47
  • @ajgo I'm glad it helped you ;) Sep 27, 2019 at 14:22
3

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]);
3
  • 1
    To keep it short for ie11 instead of "file = new File(bytes, this.document.documentName, { type: this.document.documentDataFormat });" you should do "new Blob(bytes, { type: this.document.documentDataFormat })"
    – Marco C.
    Nov 22, 2018 at 10:23
  • 1
    what does documentService.blobToFile look like?
    – SeanMC
    Jan 31, 2019 at 20:00
  • 1
    @Brahim-LAMJAGUAR's answer did it for me.
    – SeanMC
    Jan 31, 2019 at 20:20
0

This solution worked for me to create file instead of using file()

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.