需求是通过接口上传文件,我调用chooseImage拿到的临时路径:
后端期望获得的file对象:
FullStack - 【插件开发】【专治疑难杂症】【ios上架、马甲包、白包、过审、已成功上架过几百个】【多款插件已上架:https://ext.dcloud.net.cn/publisher?id=22130】【非诚勿扰】QQ:543610866
getCodeFromImg(fileAllPath){
var promise=new Promise(function(resolve,reject){
var imga = new Image();
imga.src=fileAllPath;
imga.onload=function(){
var canvas = document.createElement("canvas");
canvas.width = imga.width;
canvas.height = imga.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(imga, 0, 0, imga.width, imga.height);
var dataURL = canvas.toDataURL("image/png");
var arr = dataURL.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
console.log(new File([u8arr], "mid_shot.jpg", { type: mime }));
resolve(new File([u8arr], "mid_shot.jpg", { type: mime }));
}
imga.onerror=function(){
reject('error,图片文件转file失败')
}
});
return promise;
}
l***@163.com (作者)
小程序里面不能使用new Image加载图片进行绘图,怎么办啊?
2021-12-09 16:05
FullStack
回复 l**@163.com: function dataURLtoFile(dataurl, filename) {//将base64转换为file对象
var arr = dataurl.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], filename, {type:mime});
}
2021-12-09 17:42
一碗杨椿面
回复 l***@163.com: 解决了吗
2022-04-26 17:10
一碗杨椿面
回复 FullStack: 这个是直接使用吗 转成file
2022-04-26 17:11