我使用以下代码从文件系统中读取get文件 该代码来自一篇名为Building a File Uploader with NodeJs的博文。
当我运行我的项目时,我能够看到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获取带有代码的文件 以上?我选择文件时需要从表单中获取文件内容。
应用程序是deployed
到云端,当我使用localhost
时,我使用以下代码(有效)
const readStream = fs.createReadStream("./file.html");
...
const writeStream = sftp.createWriteStream("/index.html");
...
readStream.pipe(writeStream);
使用正确的路径从文件系统创建文件,并用另一个文件覆盖它(例如此处index.html
)。
答案 0 :(得分:0)
正如@aedneo所说,当用户选择文件时,文件是在上传文件夹中创建的,我只需要重命名它并给出写入流方法的正确路径,它就可以了!