2016-08-19 116 views
0

因此,我寫了一個基本的上傳快速上傳和它的作品,如果數據只是一個文件輸入,如果我嘗試附加一塊文本數據使用formdata。追加()它不工作,在服務器上它給未定義在服務器端的req.file.pathMulter圖片上傳不起作用 - req.file.path undefined

這裏是我的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); 
} 

The 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調用

$(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

名稱必須是與文件輸入名稱相同。所以你的情況,你有你的標記:

<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); 
}