Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript I';我发送一个文件到节点服务器,它给我404状态错误_Javascript_Node.js_Express_Fs_Multipartform Data - Fatal编程技术网

Javascript I';我发送一个文件到节点服务器,它给我404状态错误

Javascript I';我发送一个文件到节点服务器,它给我404状态错误,javascript,node.js,express,fs,multipartform-data,Javascript,Node.js,Express,Fs,Multipartform Data,我的HTML文件如下所示 文件 附属的 我正在使用node,express,如下所示 app.post("/getusrfile",function(req,res){ console.log("came to server");// prints this console.log(req.files);//prints undefined //console.log(JSON.stringify(req.files.fil)); fs.readFile(re

我的HTML文件如下所示


文件
附属的
我正在使用node,express,如下所示

app.post("/getusrfile",function(req,res){
    console.log("came to server");// prints this
    console.log(req.files);//prints undefined
    //console.log(JSON.stringify(req.files.fil));
    fs.readFile(req.files.fil, function (err, data) {
        // ...
        var newPath = __dirname + "/../public/uploadedFileName";
        fs.writeFile(newPath, data, function (err) {
            console.log(err);
            res.send(err);
        });
    });
});
它打印到服务器,但没有在服务器上创建文件/目录。 响应为“cannotpost/getusrfile”,状态为404

和req.files打印未定义

如何让它工作?

首先

    $ npm install --save multer
然后在app.post函数之前添加multer中间件:

    app.use(require('multer')({dest : __dirname }));

    app.post("/getusrfile",function(req,res){
      var filePath = req.files.fil.path;
      fs.readFile(filePath, function (err, data) {
        // ...  
      });
    });
测试:

   it('should respond "OK 200" to POST request with file payload',function(done){

        var filePath = path.join(__dirname,'test.txt');

        request(app)
        .post('/getusrfile')
        .attach('fil',filePath)
        .expect(200)
        .end(function(err,res){
            expect(err).to.not.exist;
            done();
        });
    });

这看起来像是打字错误。。。readFile(req.files.fil.path),您很可能没有使用中间件来处理文件上载(如)。