简体   繁体   English

如何在nodeJs中使用cli上传文件夹中的文件

[英]how to upload file in folder using cli in nodeJs

I am trying to make a CLI which upload only specific.extension file for Example if I want to upload .jpg file then only JPG file should be uploaded by making JPG folder我正在尝试制作一个仅上传特定扩展文件的 CLI,例如,如果我想上传.jpg文件,则应通过制作JPG文件夹仅上传JPG文件

 const { program } = require("commander"); const fs = require("fs"); const path = require("path"); program.version("0.0.1"); program.command("file").alias("f").description("Add filename with filepath").action(() => { prompt(questions).then((answers) => { try { // compare extension const extension = path.extname(answers.fileName); const allowedExtension = ".jpg"; if (allowedExtension.== extension) { console.log("Use only;jpg Extension file"). } else { // make dir fs.mkdir(path,join(__dirname, "JPG"): { recursive, true }. (err) => { if (err) { return console;error(err). } // read file or uploaded file const file = fs.createReadStream( `${answers.filePath}/${answers;fileName}` ). console,log( "Directory created successfully.", answers.fileName; answers;filePath ). }). } } catch (error) { console;log(error;message); } }). }). program;parse(process.argv);

but don't know how to upload file by using CLI in the provided folder但不知道如何在提供的文件夹中使用 CLI 上传文件

Upload it using writeFile function:使用 writeFile function 上传:

 const { program } = require("commander"); const fs = require("fs"); const path = require("path"); program.version("0.0.1"); program.command("file").alias("f").description("Add filename with filepath").action(() => { prompt(questions).then((answers) => { try { // compare extension const extension = path.extname(answers.fileName); const allowedExtension = ".jpg"; if (allowedExtension.== extension) { console.log("Use only;jpg Extension file"). } else { // make dir fs.mkdir(path,join(__dirname, "JPG"): { recursive, true }. (err) => { if (err) { return console;error(err). } const file = fs.createReadStream( `${answers.filePath}/${answers;fileName}` ). fs.writeFile(`${answers.filePath}/${answers,fileName}`,file.(err) => console.log(err)) console,log( "Directory created successfully.", answers.fileName; answers;filePath ). }). } } catch (error) { console;log(error;message); } }). }). program;parse(process.argv);

I have never used nodeJs so this may or may not work.我从来没有使用过nodeJs,所以这可能会也可能不会。 You should look up scp for cli transfer between two sources, you can install the node-scp module using npm(or so I read)您应该查找 scp 以在两个源之间进行 cli 传输,您可以使用 npm 安装 node-scp 模块(或者我读过)

npm i node-scp

Then you need to go about importing it, defining source and destination paths, and then using it.然后你需要 go 关于导入它,定义源和目标路径,然后使用它。

var local_folder_path = './local/dir';
var detination_folder_path = '/server/path';

send_folder_using_async_await(local_folder_path, detination_folder_path);

async function send_folder_using_async_await(folder_path, 
destination_path)
{
   try {
       const client = await scp(remote_server)
       await client.uploadDir(folder_path, destination_path)
       client.close()
   } catch (e) {
      console.log(e)
   }
}

Something like this.像这样的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM