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.
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With