话不多说,几行代码就可以搞定~
// 将url转成file对象
const responseFile = await fetch(imgUrlPath, { mode: 'no-cors' });
const blob = await responseFile.blob();
// 从 URL 提取文件名
const fileName = imgUrlPath.substring(imgUrlPath.lastIndexOf('/') + 1);
// 将 Blob 转换为 File 对象
const fileOne = new File([blob], fileName, { type: blob.type });
// 打印 File 对象
console.log(" File:", fileOne);
说明:imgUrlPath为存储图片url的变量
如果大佬有话要说,欢迎评论区留言哦~