我想使用按钮下载存储在
public
文件夹中的文件。我在 test.xlsx
文件夹中有 public
,这是我的代码
<a href={"/test.xlsx"} download target='_blank'>
<button> click </button>
</a>
但是,当我访问该网站时,它显示
Failed - No file
。我该如何修复它?我需要编辑吗webpack.config.js
?
你可以试试这个方法吗,应该有效。使用文件保护程序挂钩。
import React from "react";
import { saveAs } from "file-saver";
export default function App() {
const saveFile = () => {
saveAs(
"https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
"example.pdf"
);
};
return (
<div>
<button onClick={saveFile}>download</button>
</div>
);
}
您的代码 100% 有效,但您只需要下载
<a href="/test.xlsx" download ><button> click </button></a>