Node/Express: File not downloading using fs.pipe()











up vote
0
down vote

favorite












I'm having an issue downloading files to my local machine using fs.pipe() in my Node.js/Express cloud foundry application.



Basically, a POST request is sent to my server side code containing the file name my user wants to download. I access the file using the GET command from the npm module ssh2-sftp-client. Finally, this file gets saved to the users local downloads folder using the npm module downloads-folder to identify this location. The code looks like this:



app.post('/download-file', function(req, res) {

// Declare the files remote and local path as a variable.
const remoteFilename = 'invoice/csv/' + req.body.file;
const localFilename = downloadsFolder() + '/' + req.body.file;

// Use the SFTP GET command to get the file passing its remote path variable.
sftp.get(remoteFilename).then((stream) => {

// Download the file to the users machine.
stream.pipe(fs.createWriteStream(localFilename));

// Redirect user.
res.redirect('/invoice')
});
})


This works perfectly when running locally and the file gets downloaded with no issues. As this screenshot shows, the output for the destination file path is this:



Local



However, when I push this to our cloud foundry provider using cf push, the application still works fine but when I want to download the file it fails. I get no errors when error catching, the only thing thats changed is that the output for the destination file path has changed to:



Hosted



I have no idea why this is, this code works fine in Chrome, Safari when running locally but when hosted doesn't do anything. Can anyone explain what's going wrong here?



Many thanks,
G










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm having an issue downloading files to my local machine using fs.pipe() in my Node.js/Express cloud foundry application.



    Basically, a POST request is sent to my server side code containing the file name my user wants to download. I access the file using the GET command from the npm module ssh2-sftp-client. Finally, this file gets saved to the users local downloads folder using the npm module downloads-folder to identify this location. The code looks like this:



    app.post('/download-file', function(req, res) {

    // Declare the files remote and local path as a variable.
    const remoteFilename = 'invoice/csv/' + req.body.file;
    const localFilename = downloadsFolder() + '/' + req.body.file;

    // Use the SFTP GET command to get the file passing its remote path variable.
    sftp.get(remoteFilename).then((stream) => {

    // Download the file to the users machine.
    stream.pipe(fs.createWriteStream(localFilename));

    // Redirect user.
    res.redirect('/invoice')
    });
    })


    This works perfectly when running locally and the file gets downloaded with no issues. As this screenshot shows, the output for the destination file path is this:



    Local



    However, when I push this to our cloud foundry provider using cf push, the application still works fine but when I want to download the file it fails. I get no errors when error catching, the only thing thats changed is that the output for the destination file path has changed to:



    Hosted



    I have no idea why this is, this code works fine in Chrome, Safari when running locally but when hosted doesn't do anything. Can anyone explain what's going wrong here?



    Many thanks,
    G










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm having an issue downloading files to my local machine using fs.pipe() in my Node.js/Express cloud foundry application.



      Basically, a POST request is sent to my server side code containing the file name my user wants to download. I access the file using the GET command from the npm module ssh2-sftp-client. Finally, this file gets saved to the users local downloads folder using the npm module downloads-folder to identify this location. The code looks like this:



      app.post('/download-file', function(req, res) {

      // Declare the files remote and local path as a variable.
      const remoteFilename = 'invoice/csv/' + req.body.file;
      const localFilename = downloadsFolder() + '/' + req.body.file;

      // Use the SFTP GET command to get the file passing its remote path variable.
      sftp.get(remoteFilename).then((stream) => {

      // Download the file to the users machine.
      stream.pipe(fs.createWriteStream(localFilename));

      // Redirect user.
      res.redirect('/invoice')
      });
      })


      This works perfectly when running locally and the file gets downloaded with no issues. As this screenshot shows, the output for the destination file path is this:



      Local



      However, when I push this to our cloud foundry provider using cf push, the application still works fine but when I want to download the file it fails. I get no errors when error catching, the only thing thats changed is that the output for the destination file path has changed to:



      Hosted



      I have no idea why this is, this code works fine in Chrome, Safari when running locally but when hosted doesn't do anything. Can anyone explain what's going wrong here?



      Many thanks,
      G










      share|improve this question













      I'm having an issue downloading files to my local machine using fs.pipe() in my Node.js/Express cloud foundry application.



      Basically, a POST request is sent to my server side code containing the file name my user wants to download. I access the file using the GET command from the npm module ssh2-sftp-client. Finally, this file gets saved to the users local downloads folder using the npm module downloads-folder to identify this location. The code looks like this:



      app.post('/download-file', function(req, res) {

      // Declare the files remote and local path as a variable.
      const remoteFilename = 'invoice/csv/' + req.body.file;
      const localFilename = downloadsFolder() + '/' + req.body.file;

      // Use the SFTP GET command to get the file passing its remote path variable.
      sftp.get(remoteFilename).then((stream) => {

      // Download the file to the users machine.
      stream.pipe(fs.createWriteStream(localFilename));

      // Redirect user.
      res.redirect('/invoice')
      });
      })


      This works perfectly when running locally and the file gets downloaded with no issues. As this screenshot shows, the output for the destination file path is this:



      Local



      However, when I push this to our cloud foundry provider using cf push, the application still works fine but when I want to download the file it fails. I get no errors when error catching, the only thing thats changed is that the output for the destination file path has changed to:



      Hosted



      I have no idea why this is, this code works fine in Chrome, Safari when running locally but when hosted doesn't do anything. Can anyone explain what's going wrong here?



      Many thanks,
      G







      node.js express fs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      George

      204




      204
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted











          // Download the file to the users machine.




          It doesn't do that, though: it downloads the file to the machine on which the server code is running. That's why it seems to work when you run the server on localhost, because the server machine and the user machine are the same.



          Offering the file as a download involves streaming the file through the response object, making sure that you set the correct header. Something like this:



          sftp.get(remoteFilename).then((stream) => {
          res.set('content-disposition', `attachment; filename="${ req.body.file }"`);
          stream.pipe(res);
          });


          When offering a file as "attachment", it typically opens the download window of the browser and the browser stays on the same page. You can't both stream and perform a redirect, but because the browser doesn't change the URL, that should be okay.



          Also, you have no control over where the user will download the file to. It may be the downloads folder, but they are free to chose another folder.






          share|improve this answer





















          • Thank you Robert! I'll do my reading on the whole response and headers topic - I'm very new to all this server side stuff so apologies for the silly mistakes made. Your solution works perfect so thank you!
            – George
            yesterday











          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',
          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%2f53372086%2fnode-express-file-not-downloading-using-fs-pipe%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








          up vote
          1
          down vote



          accepted











          // Download the file to the users machine.




          It doesn't do that, though: it downloads the file to the machine on which the server code is running. That's why it seems to work when you run the server on localhost, because the server machine and the user machine are the same.



          Offering the file as a download involves streaming the file through the response object, making sure that you set the correct header. Something like this:



          sftp.get(remoteFilename).then((stream) => {
          res.set('content-disposition', `attachment; filename="${ req.body.file }"`);
          stream.pipe(res);
          });


          When offering a file as "attachment", it typically opens the download window of the browser and the browser stays on the same page. You can't both stream and perform a redirect, but because the browser doesn't change the URL, that should be okay.



          Also, you have no control over where the user will download the file to. It may be the downloads folder, but they are free to chose another folder.






          share|improve this answer





















          • Thank you Robert! I'll do my reading on the whole response and headers topic - I'm very new to all this server side stuff so apologies for the silly mistakes made. Your solution works perfect so thank you!
            – George
            yesterday















          up vote
          1
          down vote



          accepted











          // Download the file to the users machine.




          It doesn't do that, though: it downloads the file to the machine on which the server code is running. That's why it seems to work when you run the server on localhost, because the server machine and the user machine are the same.



          Offering the file as a download involves streaming the file through the response object, making sure that you set the correct header. Something like this:



          sftp.get(remoteFilename).then((stream) => {
          res.set('content-disposition', `attachment; filename="${ req.body.file }"`);
          stream.pipe(res);
          });


          When offering a file as "attachment", it typically opens the download window of the browser and the browser stays on the same page. You can't both stream and perform a redirect, but because the browser doesn't change the URL, that should be okay.



          Also, you have no control over where the user will download the file to. It may be the downloads folder, but they are free to chose another folder.






          share|improve this answer





















          • Thank you Robert! I'll do my reading on the whole response and headers topic - I'm very new to all this server side stuff so apologies for the silly mistakes made. Your solution works perfect so thank you!
            – George
            yesterday













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted







          // Download the file to the users machine.




          It doesn't do that, though: it downloads the file to the machine on which the server code is running. That's why it seems to work when you run the server on localhost, because the server machine and the user machine are the same.



          Offering the file as a download involves streaming the file through the response object, making sure that you set the correct header. Something like this:



          sftp.get(remoteFilename).then((stream) => {
          res.set('content-disposition', `attachment; filename="${ req.body.file }"`);
          stream.pipe(res);
          });


          When offering a file as "attachment", it typically opens the download window of the browser and the browser stays on the same page. You can't both stream and perform a redirect, but because the browser doesn't change the URL, that should be okay.



          Also, you have no control over where the user will download the file to. It may be the downloads folder, but they are free to chose another folder.






          share|improve this answer













          // Download the file to the users machine.




          It doesn't do that, though: it downloads the file to the machine on which the server code is running. That's why it seems to work when you run the server on localhost, because the server machine and the user machine are the same.



          Offering the file as a download involves streaming the file through the response object, making sure that you set the correct header. Something like this:



          sftp.get(remoteFilename).then((stream) => {
          res.set('content-disposition', `attachment; filename="${ req.body.file }"`);
          stream.pipe(res);
          });


          When offering a file as "attachment", it typically opens the download window of the browser and the browser stays on the same page. You can't both stream and perform a redirect, but because the browser doesn't change the URL, that should be okay.



          Also, you have no control over where the user will download the file to. It may be the downloads folder, but they are free to chose another folder.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          robertklep

          132k17226238




          132k17226238












          • Thank you Robert! I'll do my reading on the whole response and headers topic - I'm very new to all this server side stuff so apologies for the silly mistakes made. Your solution works perfect so thank you!
            – George
            yesterday


















          • Thank you Robert! I'll do my reading on the whole response and headers topic - I'm very new to all this server side stuff so apologies for the silly mistakes made. Your solution works perfect so thank you!
            – George
            yesterday
















          Thank you Robert! I'll do my reading on the whole response and headers topic - I'm very new to all this server side stuff so apologies for the silly mistakes made. Your solution works perfect so thank you!
          – George
          yesterday




          Thank you Robert! I'll do my reading on the whole response and headers topic - I'm very new to all this server side stuff so apologies for the silly mistakes made. Your solution works perfect so thank you!
          – George
          yesterday


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372086%2fnode-express-file-not-downloading-using-fs-pipe%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

          Volksrepublik China

          How to test boost logger output in unit testing?

          Write to the output between two pipeline