[英]Download a file from URL with different file name
我使用JavaScript具有以下功能
后端給了我一個 URL 從下面下載文件 -
https://something.org/FILENAME.ZIP
所以我的文件名固定為FILENAME.ZIP
但是在下載文件時,應該使用自定義文件名下載文件,例如CUSTOMFILENAME.ZIP而不是FILENAME.ZIP
所以,
1)文件應該從URL下載: https://something.org/FILENAME.ZIP
2)但它應該以不同的文件名保存為: CUSTOMFILENAME.ZIP
3)我已經從 url 嘗試了以下程序 - 如何使用 javascript 或 ZD2263E143914864E 從 url 下載文件?
var a = document.createElement("a");
a.href = url;
var fileName = CUSTOMFILENAME;
a.download = fileName;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
a.remove();
但是仍然會使用URL 中提到的任何文件名下載文件
有人可以在這里幫助我嗎?
該文件應使用我提供的CUSTOMFILENAME保存,而不是作為URL 的一部分指定的文件名
你給一個 object 作為下載名稱給一個字符串
試試這個看看是否有效
const documentName = CustomFileName // here your file name
fetch(
"http: url // here your url
{
method: "GET"
}
)
.then(res => {
return res.blob();
})
.then(blob => {
const href = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.download = documentName;
a.href = href;
a.click();
a.href = "";
})
.catch(err => console.error(err));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.