简体   繁体   English

如何将文件上传到JavaScript脚本

[英]How to upload a file to a javascript script

I'm writing a simple JS script which gets a file as input and does some manipulation to it: 我正在编写一个简单的JS脚本,该脚本将文件作为输入并对其进行一些处理:

node script.js file

The file part is accessed directly by using: 通过使用以下命令直接访问文件部分:

process.argv[2]

Now all i want is given that input, find that file and read it line by line into my function. 现在我想要的就是输入,找到该文件并将其逐行读入我的函数中。 I have a really hard time with this seemingly simple task since all solutions I found were HTML based. 我很难完成这个看似简单的任务,因为我发现的所有解决方案都是基于HTML的。

I need this to run as an independent script. 我需要它作为独立脚本运行。

You can do it using fs and readline : 您可以使用fsreadline

const readline = require('readline');
const fs = require('fs');

const rl = readline.createInterface({
  input: fs.createReadStream(/* Path to your file */),
  crlfDelay: Infinity
});

rl.on('line', (line) => {
  console.log(`Line from file: ${line}`);
});

cf documentation for details, fs & readline cf文档,了解详细信息, fsreadline

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

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