使用formdata但不使用库在nodejs中上传图像

时间:2019-05-31 18:27:18

标签: node.js xmlhttprequest multipartform-data image-uploading

我想在不使用库的情况下在nodejs上上传图像。我正在使用通过XMLHttpRequest发送到服务器的FormData

这是我的客户代码

app.actions.upload = function(file){   

    var formData = new FormData();
    console.log(file);
    formData.append("file", file);
    for (var p of formData) {
      console.log(p);
    }
    var xhr = new XMLHttpRequest();

    xhr.open("POST", "/upload");
    xhr.setRequestHeader('Content-Type', 'multipart/form-data');

    xhr.send(formData);
     xhr.onload = function(){
        console.log('yeffff');
    }; 

};

这就是我试图在服务器中获取文件的方式

if(trimmedPath == 'upload'){
        console.log('I get here');


            if(req.file){ // this test is not validate
            console.log('But never get there'); 
            //write the file on disc
        }
        res.end();    




    }

不幸的是,在服务器中,我无法获取文件。的  req.fileundefined

0 个答案:

没有答案
相关问题