下面的node.js服务器代码允许客户端将文件上传到临时位置。
var restify = require('restify');
var server = restify.createServer();
server.use(restify.bodyParser());
server.post('/fileupload', function(req, res, next){
var path_temp = req.files.file.path;
console.log(path_temp);
res.end('upload');
next();
});
server.listen(8000);
上传的文件存储在文件夹位置path_temp
。如何将此文件复制到正在运行的node.js脚本的当前文件夹中?
答案 0 :(得分:1)
感谢评论中提供的提示,以下是我自己问题的答案。
var restify = require('restify');
var fs = require('fs-extra');
var server = restify.createServer();
server.use(restify.bodyParser());
server.post('/fileupload', function(req, res, next){
var path_temp = req.files.file.path;
var currentFolder = process.cwd();
var filename = 'filename.txt'; //up to you
fs.move(path_temp, currentFolder + '/' + filename, function(err) {
if (err) return console.error(err)
console.log("file uploaded!")
});
res.end('upload');
next();
});
server.listen(8000);