Multer图像上传无效 - req.file.path undefined

时间:2016-08-19 02:40:38

标签: express multer

所以我用express编写了一个基本的multer上传文件,如果数据只是一个文件输入,它可以工作,如果我尝试使用formdata.append()附加一段额外的文本数据,它就不起作用了服务器它在服务器端提供了一个未定义的req.file.path

这是我的Multer设置

var upload = multer({ dest: 'uploads/' })

express.post()

app.post("/rest/OCR", upload.single('image'), function(req, res, next){
  console.log("Receiving File")
  console.log(req.file.path);
}

HTML表单

<form action='/rest/ocr' id='imageUploadForm' method='post' enctype='multipart/form-data'>
    <input class='vwide upload-button' type='file' name='file'>

    <input type='submit'>
</form>

JS Call

 $(document.body).on('submit', '#imageUploadForm', function(e){
        e.preventDefault();
        var self = this;
        var data = new FormData();
        data.append('id', cardlob.profile.auth.id);
        data.append('file', $(this)[0]);
        $.ajax({
            processData: false, 
            cache: false,
            async: false, 
            data: data,
            url: "/rest/OCR",
            type: 'POST',
            success: function(data, textStatus, jqXHR){
                var cardDto = JSON.parse(data);
                if(cardDto.vCardFormattedString !== "null"){
                    window.open("/cards/"+cardDto.hash+".vcf");
                }else{
                    $("#textData").append("<h4> No Business Cards Found in image </h4>");
                }
            }
        });
    });

我无法告诉我们要做出哪些未定义的

1 个答案:

答案 0 :(得分:1)

.single()调用中属性的名称必须与文件输入的名称相同。所以在你的情况下,你有你的标记:

<input class='vwide upload-button' type='file' name='file'>

所以你的路由器中间件应该是:

app.post("/rest/OCR", upload.single('file'), function(req, res, next){
  console.log("Receiving File")
  console.log(req.file.path);
}