Node.js 从命令行界面上传文件
Node.js file upload from command line interface
我正在为我们的应用程序创建自定义命令行界面 (CLI)。现在,我需要从 CLI 上传一个文件。我们可以从 CLI 上传文件吗?假设我在终端的项目文件夹位置 abc
中,它包含一个 .js
文件。我想获取该文件并上传到服务器。
我使用 multer with express 框架从浏览器以 html 形式上传文件。但我不知道如何从命令行界面执行此操作。任何帮助或建议将不胜感激。
写一个我自己的问题的答案,因为没有人发布答案。按照评论部分的@hassansin 指南和 this 解决了我的问题。感谢 Whosebug。
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);
});
我正在为我们的应用程序创建自定义命令行界面 (CLI)。现在,我需要从 CLI 上传一个文件。我们可以从 CLI 上传文件吗?假设我在终端的项目文件夹位置 abc
中,它包含一个 .js
文件。我想获取该文件并上传到服务器。
我使用 multer with express 框架从浏览器以 html 形式上传文件。但我不知道如何从命令行界面执行此操作。任何帮助或建议将不胜感激。
写一个我自己的问题的答案,因为没有人发布答案。按照评论部分的@hassansin 指南和 this 解决了我的问题。感谢 Whosebug。
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);
});