//下载并上传文件
// 参数 下载url、上传url、文件名称、文件类型
try {
var res = await fetch(downloadUrl, {
method: ‘GET’
});
var blob = await res.blob();
var newBlob = new Blob([blob]);
var formData = new FormData();
formData.append(“file”, new File([newBlob], ‘test.png’, {type: ‘image/png’}));
var resp = await fetch(uploadUrl, {
“body”: formData,
“method”: “POST”
});
console.log(resp);
}catch(error){
console.log(error);
}
//下载保存为文件
// 参数 下载url、文件名称
try {
const res = await fetch(url, {
method: ‘GET’,
“headers”: {
“accept”: “image/avif,image/webp,image/apng,image/svg+xml,image/,/*;q=0.8”,
“accept-language”: “zh-CN,zh;q=0.9”,
“cache-control”: “no-cache”,
“pragma”: “no-cache”
}
});
const blob = await res.blob();
const newBlob = new Blob([blob]);
const newUrl = window.URL.createObjectURL(newBlob);
const link = document.createElement(‘a’);
link.href = newUrl;
link.setAttribute(‘download’, ‘filename.png’);
document.body.appendChild(link);
link.click();
link.parentNode.removeChild(link);
window.URL.revokeObjectURL(newBlob);
}catch(error) {
console.log(error);
}
#下载返回文件
// 参数 下载url、文件名称、文件URL
try {
const res = await fetch(url, {
method: ‘GET’,
“headers”: {
“accept”: “image/avif,image/webp,image/apng,image/svg+xml,image/,/*;q=0.8”,
“accept-language”: “zh-CN,zh;q=0.9”,
“cache-control”: “no-cache”,
“pragma”: “no-cache”
}
});
const blob = await res.blob();
const newBlob = new Blob([blob]);
return new File([newBlob], ‘test.png’, {type: ‘image/png’});ss
}catch(error) {
console.log(error);
}
备注fetch兼容性:https://www.cnblogs.com/lhyxq/p/9779076.html