const uploadImg = async (url: string) => {
let formData = new FormData();
let blob = dataURLtoFile(url, 'image/jpeg');
let fileOfBlob = new File([blob], new Date().getTime() + '.jpg', {
type: 'image/jpeg'
}); // 命名图片名
formData.append('file', fileOfBlob);
uni.uploadFile({
url: import.meta.env.VITE_BASR_URL + '/system/file/upload', //仅为示例,非真实的接口地址
formData: formData,
success: (uploadFileRes) => {
console.log(uploadFileRes.data);
}
});
};
一开始这么写,发现上传成功后端获取不到文件name,后端通过name来判断文件类型的,这么写name丢失,服务端报错,
const uploadImg = async (url: string) => {
let blob = dataURLtoFile(url, 'image/jpeg');
let fileOfBlob = new File([blob], new Date().getTime() + '.jpg', {
type: 'image/jpeg'
}); // 命名图片名
uni.uploadFile({
url: import.meta.env.VITE_BASR_URL + '/system/file/upload', //仅为示例,非真实的接口地址
formData: {
'file': fileOfBlob
},
success: (uploadFileRes) => {
console.log(uploadFileRes.data);
}
});
};
修改后的写法,不需要再将blob对象转化成formdata对象,直接将blob对象配置再formData配置中就行。