[英]button to download a file in reactJS
我目前正在處理個人投資組合,我正在嘗試制作一個按鈕,如果您單擊它應該下載簡歷。
代碼
<form method="get" action="fileName">
<button class="myButton" type="submit">Download!</button>
</form>
讓用戶下載文件。
我在 REACTJS 工作,create-react-app
你可以這樣做
<a href="./yourfile.pdf" download>Download CV</a>
您可以使用FileSaver.js來實現此目標:
安裝 npm package: npm install file-saver --save
const saveFile = () => {
fileSaver.saveAs(
process.env.REACT_APP_CLIENT_URL + "/resources/cv.pdf",
"MyCV.pdf"
);
};
<button className="cv" onClick={saveFile}>
Download File
</button>
我有完全相同的問題,這是我現在使用的解決方案:
3個步驟:
npm install file-saver
什么的)resource
或asset
名稱下的public
中。 Webpack 不會觸及public
和index.html
並且您的資源會按原樣在生產構建中被復制,您可以在其中引用它們,如下一步所示。 import FileSaver from 'file-saver'; FileSaver.saveAs( process.env.PUBLIC_URL + "/resource/file.anyType", "fileNameYouWishCustomerToDownLoadAs.anyType");
react-router
react-router-docs/Link 的Link
組件。 zip 文件將下載,並以某種方式正確解壓縮。 通常,鏈接具有藍色,要繼承父配色方案,只需添加一個道具,如: style={color: inherit}
或簡單地分配 CSS 庫的 class ,如button button-primary
或其他東西,如果你是引導程序沒用,這里仍然很好的答案:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.