从表单上传文件中读取文件内容

我使用下面的代码从文件系统中读取获取文件代码来自一篇名为使用NodeJs构buildfile upload器的博客文章。

当我运行我的项目时,我能看到UI等。

我不能使用下面的代码,因为我的项目中没有uploads文件夹( form.uploadDir

 app.post('/upload', function(req, res){ // create an incoming form object var form = new formidable.IncomingForm(); // specify that we want to allow the user to upload multiple files in a single request form.multiples = true; // store all uploads in the /uploads directory - cannot use it //form.uploadDir = path.join(__dirname, '/uploads'); // every time a file has been uploaded successfully, // rename it to it's original name form.on('file', function(field, file) { fs.rename(file.path, path.join(form.uploadDir, file.name)); }); // log any errors that occur form.on('error', function(err) { console.log('An error has occurred: \n' + err); }); // once all the files have been uploaded, send a response to the client form.on('end', function() { res.end('success'); }); // parse the incoming request containing the form data form.parse(req); }); 

我的问题是如何从上面的代码从UI获取文件? 当我select我的文件时,我需要从表单中获取文件内容。

该应用程序deployed到云,当我使用localhost ,我使用下面的代码(工作)

 const readStream = fs.createReadStream("./file.html"); 

 const writeStream = sftp.createWriteStream("/index.html"); 

 readStream.pipe(writeStream); 

它使用正确的path从文件系统创build一个文件,并用另一个文件(如index.html )覆盖它。

正如@aedneo所说的,当用户select文件的时候,这个文件就是在上传文件夹中创build的,我只需要重命名它,并给出写入stream方法的正确path,它的工作原理!