Safari和IE是否有File()构造函数的替代方法? [英] Is there an alternative for File() constructor for Safari and IE?

查看:179
本文介绍了Safari和IE是否有File()构造函数的替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用File()构造函数创建用于将Blob文件上传到服务器的文件对象.以下代码在Chrome上运行正常,但在Safari和Internet Explorer上运行失败.

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

代码在此行中断,并在控制台"FileConstructor不是构造函数"中得到此错误(正在评估'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})')

使用FileReader API可以替代此方法,但是我无法解决此问题.

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

推荐答案

我建议使用Blob api,我发现了同样的问题,并按以下方式解决了该问题:

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.

这篇关于Safari和IE是否有File()构造函数的替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆