multiple PDF set header










0















I have this in a loop but I have multiple files that I'd like to generate. I'm receiving Cannot set headers after they are sent to the client.



If I have one key, then it's fine. If I have more than one, it generates the second one, doesn't generate the first one and gives this error.



 _.forEach(keys, async function(key) 
try
const pdf = await myPDFd(key);

res.setHeader('Content-disposition', 'attachment; filename=' + key + '.pdf');
res.setHeader('content-type', 'application/pdf');
res.send(pdf);
res.end();
catch (error)

);









share|improve this question

















  • 2





    The problem is that you can't send two responses to one request (res.send is sending the response on every loop step). Maybe you could compress the files and then send the response.

    – Ariel Alvarado
    Nov 15 '18 at 15:02












  • @ArielAlvarado is there a way to stop the response after one pdf is send? I thought res.end would do so and create a new one on the next one?

    – jaimers
    Nov 15 '18 at 15:32






  • 1





    You cannot initiate the second connection. Server cannot initiate connection to client (not by HTTP). Second connection initialization must comes from client to server (same way as a first connection).

    – Martin
    Nov 15 '18 at 15:36















0















I have this in a loop but I have multiple files that I'd like to generate. I'm receiving Cannot set headers after they are sent to the client.



If I have one key, then it's fine. If I have more than one, it generates the second one, doesn't generate the first one and gives this error.



 _.forEach(keys, async function(key) 
try
const pdf = await myPDFd(key);

res.setHeader('Content-disposition', 'attachment; filename=' + key + '.pdf');
res.setHeader('content-type', 'application/pdf');
res.send(pdf);
res.end();
catch (error)

);









share|improve this question

















  • 2





    The problem is that you can't send two responses to one request (res.send is sending the response on every loop step). Maybe you could compress the files and then send the response.

    – Ariel Alvarado
    Nov 15 '18 at 15:02












  • @ArielAlvarado is there a way to stop the response after one pdf is send? I thought res.end would do so and create a new one on the next one?

    – jaimers
    Nov 15 '18 at 15:32






  • 1





    You cannot initiate the second connection. Server cannot initiate connection to client (not by HTTP). Second connection initialization must comes from client to server (same way as a first connection).

    – Martin
    Nov 15 '18 at 15:36













0












0








0








I have this in a loop but I have multiple files that I'd like to generate. I'm receiving Cannot set headers after they are sent to the client.



If I have one key, then it's fine. If I have more than one, it generates the second one, doesn't generate the first one and gives this error.



 _.forEach(keys, async function(key) 
try
const pdf = await myPDFd(key);

res.setHeader('Content-disposition', 'attachment; filename=' + key + '.pdf');
res.setHeader('content-type', 'application/pdf');
res.send(pdf);
res.end();
catch (error)

);









share|improve this question














I have this in a loop but I have multiple files that I'd like to generate. I'm receiving Cannot set headers after they are sent to the client.



If I have one key, then it's fine. If I have more than one, it generates the second one, doesn't generate the first one and gives this error.



 _.forEach(keys, async function(key) 
try
const pdf = await myPDFd(key);

res.setHeader('Content-disposition', 'attachment; filename=' + key + '.pdf');
res.setHeader('content-type', 'application/pdf');
res.send(pdf);
res.end();
catch (error)

);






javascript node.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 15:00









jaimersjaimers

6091130




6091130







  • 2





    The problem is that you can't send two responses to one request (res.send is sending the response on every loop step). Maybe you could compress the files and then send the response.

    – Ariel Alvarado
    Nov 15 '18 at 15:02












  • @ArielAlvarado is there a way to stop the response after one pdf is send? I thought res.end would do so and create a new one on the next one?

    – jaimers
    Nov 15 '18 at 15:32






  • 1





    You cannot initiate the second connection. Server cannot initiate connection to client (not by HTTP). Second connection initialization must comes from client to server (same way as a first connection).

    – Martin
    Nov 15 '18 at 15:36












  • 2





    The problem is that you can't send two responses to one request (res.send is sending the response on every loop step). Maybe you could compress the files and then send the response.

    – Ariel Alvarado
    Nov 15 '18 at 15:02












  • @ArielAlvarado is there a way to stop the response after one pdf is send? I thought res.end would do so and create a new one on the next one?

    – jaimers
    Nov 15 '18 at 15:32






  • 1





    You cannot initiate the second connection. Server cannot initiate connection to client (not by HTTP). Second connection initialization must comes from client to server (same way as a first connection).

    – Martin
    Nov 15 '18 at 15:36







2




2





The problem is that you can't send two responses to one request (res.send is sending the response on every loop step). Maybe you could compress the files and then send the response.

– Ariel Alvarado
Nov 15 '18 at 15:02






The problem is that you can't send two responses to one request (res.send is sending the response on every loop step). Maybe you could compress the files and then send the response.

– Ariel Alvarado
Nov 15 '18 at 15:02














@ArielAlvarado is there a way to stop the response after one pdf is send? I thought res.end would do so and create a new one on the next one?

– jaimers
Nov 15 '18 at 15:32





@ArielAlvarado is there a way to stop the response after one pdf is send? I thought res.end would do so and create a new one on the next one?

– jaimers
Nov 15 '18 at 15:32




1




1





You cannot initiate the second connection. Server cannot initiate connection to client (not by HTTP). Second connection initialization must comes from client to server (same way as a first connection).

– Martin
Nov 15 '18 at 15:36





You cannot initiate the second connection. Server cannot initiate connection to client (not by HTTP). Second connection initialization must comes from client to server (same way as a first connection).

– Martin
Nov 15 '18 at 15:36












1 Answer
1






active

oldest

votes


















1














Your way is not possible.



Each HTTP request responses exactly 1 type of data (one PDF file in your case). Not possible to send 2 files via 1 HTTP request.



You have to create some logic on client (two download links, for example). Client (user or app) must perform two separate actions (run two separate HTTP requests) for 2 files downloading.



Alternatively you can compress this two files to ZIP (or another package type) and send this one ZIP file to user. But in this case user (or app) will have to uncompress it as additional step. You can use node-zip library.






share|improve this answer

























  • Thanks. I'll do the zip but just curious, I thought res.end() would end the request in the loop and restart on the next?

    – jaimers
    Nov 15 '18 at 15:36











  • res.end() will completely close the connection. No additional data can be send after call res.end() and there is no possibility to reopen closed connection by res.end(). When you close the connection, the TCP HTTP connection is finished and since this time the client does not know, that you are doing any additional commands on the server.

    – Martin
    Nov 15 '18 at 15:39










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322248%2fmultiple-pdf-set-header%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your way is not possible.



Each HTTP request responses exactly 1 type of data (one PDF file in your case). Not possible to send 2 files via 1 HTTP request.



You have to create some logic on client (two download links, for example). Client (user or app) must perform two separate actions (run two separate HTTP requests) for 2 files downloading.



Alternatively you can compress this two files to ZIP (or another package type) and send this one ZIP file to user. But in this case user (or app) will have to uncompress it as additional step. You can use node-zip library.






share|improve this answer

























  • Thanks. I'll do the zip but just curious, I thought res.end() would end the request in the loop and restart on the next?

    – jaimers
    Nov 15 '18 at 15:36











  • res.end() will completely close the connection. No additional data can be send after call res.end() and there is no possibility to reopen closed connection by res.end(). When you close the connection, the TCP HTTP connection is finished and since this time the client does not know, that you are doing any additional commands on the server.

    – Martin
    Nov 15 '18 at 15:39















1














Your way is not possible.



Each HTTP request responses exactly 1 type of data (one PDF file in your case). Not possible to send 2 files via 1 HTTP request.



You have to create some logic on client (two download links, for example). Client (user or app) must perform two separate actions (run two separate HTTP requests) for 2 files downloading.



Alternatively you can compress this two files to ZIP (or another package type) and send this one ZIP file to user. But in this case user (or app) will have to uncompress it as additional step. You can use node-zip library.






share|improve this answer

























  • Thanks. I'll do the zip but just curious, I thought res.end() would end the request in the loop and restart on the next?

    – jaimers
    Nov 15 '18 at 15:36











  • res.end() will completely close the connection. No additional data can be send after call res.end() and there is no possibility to reopen closed connection by res.end(). When you close the connection, the TCP HTTP connection is finished and since this time the client does not know, that you are doing any additional commands on the server.

    – Martin
    Nov 15 '18 at 15:39













1












1








1







Your way is not possible.



Each HTTP request responses exactly 1 type of data (one PDF file in your case). Not possible to send 2 files via 1 HTTP request.



You have to create some logic on client (two download links, for example). Client (user or app) must perform two separate actions (run two separate HTTP requests) for 2 files downloading.



Alternatively you can compress this two files to ZIP (or another package type) and send this one ZIP file to user. But in this case user (or app) will have to uncompress it as additional step. You can use node-zip library.






share|improve this answer















Your way is not possible.



Each HTTP request responses exactly 1 type of data (one PDF file in your case). Not possible to send 2 files via 1 HTTP request.



You have to create some logic on client (two download links, for example). Client (user or app) must perform two separate actions (run two separate HTTP requests) for 2 files downloading.



Alternatively you can compress this two files to ZIP (or another package type) and send this one ZIP file to user. But in this case user (or app) will have to uncompress it as additional step. You can use node-zip library.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 15:41

























answered Nov 15 '18 at 15:33









MartinMartin

18923




18923












  • Thanks. I'll do the zip but just curious, I thought res.end() would end the request in the loop and restart on the next?

    – jaimers
    Nov 15 '18 at 15:36











  • res.end() will completely close the connection. No additional data can be send after call res.end() and there is no possibility to reopen closed connection by res.end(). When you close the connection, the TCP HTTP connection is finished and since this time the client does not know, that you are doing any additional commands on the server.

    – Martin
    Nov 15 '18 at 15:39

















  • Thanks. I'll do the zip but just curious, I thought res.end() would end the request in the loop and restart on the next?

    – jaimers
    Nov 15 '18 at 15:36











  • res.end() will completely close the connection. No additional data can be send after call res.end() and there is no possibility to reopen closed connection by res.end(). When you close the connection, the TCP HTTP connection is finished and since this time the client does not know, that you are doing any additional commands on the server.

    – Martin
    Nov 15 '18 at 15:39
















Thanks. I'll do the zip but just curious, I thought res.end() would end the request in the loop and restart on the next?

– jaimers
Nov 15 '18 at 15:36





Thanks. I'll do the zip but just curious, I thought res.end() would end the request in the loop and restart on the next?

– jaimers
Nov 15 '18 at 15:36













res.end() will completely close the connection. No additional data can be send after call res.end() and there is no possibility to reopen closed connection by res.end(). When you close the connection, the TCP HTTP connection is finished and since this time the client does not know, that you are doing any additional commands on the server.

– Martin
Nov 15 '18 at 15:39





res.end() will completely close the connection. No additional data can be send after call res.end() and there is no possibility to reopen closed connection by res.end(). When you close the connection, the TCP HTTP connection is finished and since this time the client does not know, that you are doing any additional commands on the server.

– Martin
Nov 15 '18 at 15:39



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322248%2fmultiple-pdf-set-header%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

27

Top Tejano songwriter Luis Silva dead of heart attack at 64

Category:Rhetoric