phuoc
Beginner
Asked At:
1632097361000
In:
React
download template xlsx trên ReactJs
Hi mn,
<p>
</p>
<p>
Em muốn khi click vào button download thì tải về file template.
</p>
<p>
</p>
<p>
E đặt file template vào cùng thư mục.
</p>
<p>
</p>
<p>
Đây là đoạn code:
</p>
<pre>import batchTemplate from "./template.xlsx";
function downloadTemplate() {
axios({
url: batchTemplate,
method: "GET",
headers: {
"Content-Disposition": "attachment; filename=template.xlsx",
"Content-Type":
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
},
responseType: "arraybuffer"
}).then(response => {
console.log(response);
const url = window.URL.createObjectURL(new Blob([response.data]));
console.log(url);
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", "template.xlsx"); //or any other extension
document.body.appendChild(link);
link.click();
});
}</pre>
<p>
Chạy local ok. Nhưng lên server thì lỗi.
</p>
<p>
</p>
<img class="alignnone size-full wp-image-1758" src="https://stackask.com/wp-content/uploads/2021/09/loi.png" alt="" />
<p>
Mong ace support. Thanks ace nhiều.
</p>