admin管理员组文章数量:1236749
I am sending data to my server that creates a pdf file based from the request, which is being created fine but I cant get the file to be sent back to the client. I am using React to submit the form
handleSubmit(event) {
event.preventDefault();
var body = {
id: this.state.id,
text: this.state.text
}
fetch('http://localhost:5000/pdf', {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json'
},
}).then(function(file) {
window.open(file.url);
});
}
Its opening http://localhost:5000/pdf, but since I don't have a GET route for it, nothing gets download. This is my POST route
router.post('/pdf', async function(req, res) {
var makePdf = require('./file-create/pdf.js');
makePdf(req, res);
});
and the file is being returned as pdfDoc.pipe(res);
I cant just use a GET route because I can't get data sent in with that way, how can I get this file to get sent to the client?
I am sending data to my server that creates a pdf file based from the request, which is being created fine but I cant get the file to be sent back to the client. I am using React to submit the form
handleSubmit(event) {
event.preventDefault();
var body = {
id: this.state.id,
text: this.state.text
}
fetch('http://localhost:5000/pdf', {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json'
},
}).then(function(file) {
window.open(file.url);
});
}
Its opening http://localhost:5000/pdf, but since I don't have a GET route for it, nothing gets download. This is my POST route
router.post('/pdf', async function(req, res) {
var makePdf = require('./file-create/pdf.js');
makePdf(req, res);
});
and the file is being returned as pdfDoc.pipe(res);
I cant just use a GET route because I can't get data sent in with that way, how can I get this file to get sent to the client?
Share Improve this question edited Aug 23, 2018 at 20:16 qaakmnd asked Aug 23, 2018 at 20:11 qaakmndqaakmnd 3692 gold badges4 silver badges18 bronze badges 2- Have you tried this?: stackoverflow./questions/34586671/… – lmarqs Commented Aug 23, 2018 at 20:20
- yes, it would download the file but no contents to it/failed to load – qaakmnd Commented Aug 23, 2018 at 20:44
1 Answer
Reset to default 13You're calling a GET request when you're using window.open
. This will open the url in a new tab with the URL. This won't work when you have changed it from GET to POST.
To get around this, you can use downloadjs
(https://www.npmjs./package/downloadjs) to allow you to download the blob that is returned from the server.
I've included some example code below. This includes the index.html file with the fetch request and the server.js for returning a simple pdf.
index.html
var body = {
id: 1,
text: 'hello world',
};
fetch('/download', {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json'
},
}).then(function(resp) {
return resp.blob();
}).then(function(blob) {
return download(blob, "CUSTOM_NAME.pdf");
});
<script src="https://cdnjs.cloudflare./ajax/libs/downloadjs/1.4.8/download.min.js"></script>
server.js
var express = require('express');
var app = express();
app.post('/download', function(req, res){
res.download('./make-file/whatever.pdf');
});
app.listen(3000);
本文标签: javascriptPost request that returns a download fileStack Overflow
版权声明:本文标题:javascript - Post request that returns a download file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739930090a2211597.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论