上传的图像未保存在Node.js中的目标路径中

时间:2015-01-17 18:11:00

标签: javascript node.js file-upload

我只是想在我的public/images文件夹中上传和保存图片,我正在通过req.files获取文件的详细信息,之后我得到了这种Error: EXDEV, rename 'c:\Users\abdul\AppData\Local\Temp\3348-qiy7kl.jpg'

这是我的stuff

 app.post('/upload',function(req,res){
    var tmp_path = req.files.file.path;
    var target_path = './public/images/' + req.files.file.originalFilename;
    fs.rename(tmp_path, target_path, function(err) {
        if (err) throw err;
        fs.unlink(tmp_path, function() {
            if (err) throw err;
            res.send('File uploaded to: ' + target_path + ' - ' + req.files.file.size + ' bytes');
        });
    });
    })
}; 

任何机构都可以提出任何建议或给我任何参考,以便我能够处理吗?

1 个答案:

答案 0 :(得分:0)

试试这个

    console.log("File Name " + req.files.file.name);
    console.log("File Path " + req.files.file.path);

    fs.readFile(req.files.file.path, function (err, data) {
        var newPath = "public/images/" + req.files.file.originalFilename;
        console.log(newPath);
        /// write file to uploads/fullsize folder
        fs.writeFile(newPath, data, function (err) {
            /// let's see it
        });