我正在嘗試通過 URL 下載檔案,但頁面正在重定向而不是下載。
我不想使用任何擴展或庫。
const File = ({href}) => {
const onDownload = () => {
const link = document.createElement('a');
link.href = href;
link.download = 'name';
link.click();
}
return (
<button onClick={onDownload}>
download
</button>
);
};
const Downloader = () => {
const files = [
'https://thumbs.dreamstime.com/b/example-red-tag-example-red-square-price-tag-117502755.jpg',
'https://image.shutterstock.com/image-photo/example-word-written-on-wooden-260nw-1765482248.jpg',
]
return (<div>{files.map(f => <File href={f} key={f}/>)} </div>);
};
export default Downloader;
uj5u.com熱心網友回復:
截至 2018 年底,如果要下載的資源不是從同一來源或同一服務器提供的,則單擊該鏈接將不會觸發下載。顯然,這是限制是一種安全措施。
您可以在瀏覽器中下載內容并使其可下載,您可以查看以下網址:
https://medium.com/charisol-community/downloading-resources-in-html5-a-download-may-not-work-as-expected-bf63546e2baa
答案來自:下載鏈接在 html 中不起作用
uj5u.com熱心網友回復:
您可以簡單地添加它來代替按鈕:
const File = ({href}) => {
return (
<a href={href} download><button>Download</button></a>
);
};
這將強制瀏覽器下載鏈接檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/507433.html