ERR_CONNECTION_REFUSED用于文件上传

时间:2018-04-20 15:25:40

标签: node.js

我正在关注w3School的文件上传教程 https://www.w3schools.com/nodejs/nodejs_uploadfiles.asp

当我尝试设置桌面路径时,它无法正常工作

app.js

var http = require('http');
var formidable = require('formidable');
var fs = require('fs');

http.createServer(function (req,res){

    if(req.url == '/fileupload'){
        var form = new formidable.IncomingForm();
        form.parse(req,function(err,fields,files){
            console.log(err);
            var oldpath = files.filetoupload.path;
            console.log(oldpath);
            var newpath = 'C:/Users/usr/Desktop/' + files.filetoupload.name;
            fs.rename(oldpath, newpath, function(err) {
                if(err){
                    throw err;
                }
                res.write('File uploaded and moved');
                res.end();
            });
        });
    }else{
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write('<form action = "fileupload" method="post" enctype="multipart/form-data">');
        res.write('<input type = "file" name = "filetoupload">');
        res.write('<input type = "submit">');
        res.write('</form>');

        return res.end();
    }

错误:

no such file or directory, rename '/var/folders/xc/l94jk0vx4cx7v2gf7fytvy600000gn/T/upload_b2ccee4542cc2b2c1e1e1475848284e2' -> 'C:/Users/usr/Desktop/Screen Shot 2018-04-19 at 2.41.23 PM.png'

我找不到问题。在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

所以这是一个愚蠢的错误。

对于Mac用户,您不必在路径中输入C:/。这只是/Users/usr/Desktop/