在Microsoft Edge中实例化文件对象



我正在尝试使用文件API从blob-object创建一个图像文件,然后将其添加到要通过XHR发送的表单中。像Chrome中的魅力一样工作,但在Microsoft Edge中崩溃了。

let file = new File([blobContent], "image.png");
let form = new FormData();
form.append("file", file);

文件API或解决方法是否有任何替代方案将文件附加到表单上?如果我只是将斑点添加到表格中,则不会被识别为图像。

谢谢!

当前IE11,Edge支持FELEAPI,但不支持文件构造函数。

在您发布到caniuse.com的链接中,有IE和Edge的注释,指出不支持文件构造函数。我遇到了同样的问题,我的工作是使用Blob而不是File,然后设置斑点的类型。

var blob = new Blob([blobContent], {type : 'image/jpeg'});

这是我所做的,可以工作

// to change Blob to File
private blobToFile(theBlob: Blob, fileName: string): File {
   const b: any = theBlob;
   b.lastModifiedDate = new Date();
   b.name = fileName;
   return b as File;
}
let file;
if (!navigator.msSaveBlob) { // detect if not Edge
   file = new File([b], document.originalName, { type: document.mimeType });
} else {
   file = new Blob([b], { type: document.mimeType });
   file = this.blobToFile(file, document.originalName);
}

现在可以通过变量文件作为边缘或其他浏览器中的文件继续进行。

最新更新