2016-08-19 15 views
0

私は基本的なmulterアップロードをexpressで書いており、formdataを使って追加のテキストデータを添付しようとすると、データがファイル入力のみであれば動作します。ここに私のMulter設定multer image upload not working - req.file.path undefined

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); 
} 
を、それは、サーバー側で未定義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コール

$(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>"); 
       } 
      } 
     }); 
    }); 

私がでなければなりません.single()の呼び出しで属性のこの未定義

答えて

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