P粉0205562312023-09-02 19:51:17
我們可以設定頭部 Content-disposition
attachment
來指示回應是一個可下載的檔案。
後端:Express範例
const htmlToPDF = new HTMLToPDF(` <div>Hello world</div> `); const buffer = await htmlToPDF.convert(); res.set("Content-Disposition", `attachment; filename="test.pdf"`); res.set("Content-Type", "application/pdf"); res.send(buffer);
前端:React範例
const submit = () => { window.open("http://localhost:8000"); // 在此处填写您的端点 }; return ( <button onClick={submit}>下载</button> );
如果端點是POST方法,則 window.open
將無法運作。我們必須使用一個表單:
<form action="http://localhost:8000" method="POST"> <button type="submit">下载</button> </form>