Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js file upload from command line interface

I am creating custom command line interface(CLI) for our application. Now, I need to upload a file from CLI. Can we upload a file from CLI? Let say I'm in project folder location abc in terminal and it contains a .js file. I want to get that file and upload into server.

I used multer with express framework for uploading files from browser with html form. But I don't know how to do this from command line interface. Any help or suggestion would be much appreciated.

like image 650
iLaYa ツ Avatar asked Oct 20 '25 05:10

iLaYa ツ


1 Answers

Writing an answer to my own question as nobody has posted an answer. Followed @hassansin guide from comment section and this, that solved my problem. Thanks stackoverflow.

var formData = {
    file: fs.createReadStream('local/file/path/test.js'),
};

request.post(
    { url:'http://server/file/upload/url', 
      headers:{ 'X-SessionToken-Key':'XXXXXXXXX',
                'Content-Type':'multipart/form-data'},
      formData: formData
    }, function optionalCallback(err, httpResponse, body) {
          if (err) {
            return console.error('upload failed:', err);
          }
        console.log('Upload successful!  Server responded with:', body);
    });
like image 169
iLaYa ツ Avatar answered Oct 21 '25 20:10

iLaYa ツ