Is there an alternative for File() constructor for Safari and IE?

Johny me picture Johny me · Nov 20, 2015 · Viewed 8.6k times · Source

I am using the File() constructor for creating file object for uploading a blob file to the server. The following code works fine for Chrome, but fails for Safari and Internet Explorer.

image_url = new File([blob],file_name,{type: mimeString});

The code is breaking at this line and getting this error in console "FileConstructor is not a constructor" (evaluating 'new File([blob],file_name,{type: mimeString})')

Using the FileReader API is an alternative to this but I am not able to fix this issue.

Answer

BrunoLoops picture BrunoLoops · Apr 1, 2016

I Suggest to use the blob api, I've found the same problem and I solved like that:

var html = <svg>whatever on svg </svg>
var fileName = "myfile.svg";
var blob = new Blob([html], {type: 'image/svg'});
blob.lastModifiedDate = new Date();
// var blobAttrs = {type: "image/svg"};
// var file = new File([html], fileName, blobAttrs);
var formData = new FormData();
formData.append("file",blob,fileName);

It is not a "file", but you can use it like it was.