P粉0205562312023-09-02 19:51:17
We can set the header Content-disposition
attachment
to indicate that the response is a downloadable file.
Backend: Express Example
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);
Front end: React example
const submit = () => { window.open("http://localhost:8000"); // 在此处填写您的端点 }; return ( <button onClick={submit}>下载</button> );
If the endpoint is a POST method, window.open
will not work. We have to use a form:
<form action="http://localhost:8000" method="POST"> <button type="submit">下载</button> </form>