最近公司要求统一http框架 需要脱离uploader改用普通的formdata上传文件,大体思路就是将plus的file对象转为blob然后重新生成符合formdata格式的file对象 代码如下:
function chooseFile(filter) {
var that = this;
plus.gallery.pick(function (path) {
plus.io.resolveLocalFileSystemURL(path, function (entry) {
entry.file(function (file) {
var reader = new plus.io.FileReader();
reader.onloadend = function (e) {
var file=(function (path,name) {
var arr = path.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], name, {type: mime});
})(e.target.result,entry.name);
var formData = new FormData();
formData.append("file", file);
that.upload(formData);
};
reader.readAsDataURL(file);
});
}, function (e) {
console.log(e)
});
}, function (e) {
console.log(e);
}, {filter: filter, system: true});
},
2 个评论
要回复文章请先登录或注册
2***@qq.com
7***@qq.com