2017-08-06 4 views
0

イメージをアップロードするのにmulterを使用していて、うまく動作していますが、データがコンソールに出力されているのがわかりましたが、このデータをキャプチャして操作する方法がわかりませんそれはブラウザで。以下は私のコードです:ajaxがmulterのNode Expressからレスポンスをキャプチャできません

var multer = require("multer"); 
var storage = multer.diskStorage({ 
    destination: function(req, file, callback){ 
     callback(null, 'uploads'); // set the destination 
    }, 
    filename: function(req, file, callback){ 
     callback(null, 'FILE1' + '.jpg'); // set the file name and extension 
    } 
}); 
var upload = multer({storage: storage}); 
app.post('/', upload.any(), function(req, res, next) { 
    console.log(req.files); 
    var image = req.files; 
    console.log(image); 
    res.send(image); //this should come to jquery response object in browser, but not sure why it is not 
}); 

と以下のレスポンスオブジェクトのデータの上に得ることができる必要があります私のjqueryのコードです:

$('body').on('click', '#uploadFile', function (event) { 

      $.ajax({ 
       type: 'POST', 
       url: '/', 
       success: function (response) { //this is always "" not sure why, I am expecting multer response to be here 
        console.log('successfully uploaded', response); 
       }, 
       error: function() { 
        console.log('coudn\'t upload'); 
       } 
      }); 
      //return false; 
     }) 

私はこの問題は、時間によって私は応答をキャプチャすることだと思いますjquery、multerはファイルをアップロードしていないので、私はajaxで空白の応答がありません。私はこれを解決する方法がわかりません。

+0

[ネットワーク]タブをチェックして、実際に応答として得たものを確認しましたか? jQueryはレスポンスを解析しようとします。そのため、レスポンスは空になります。 –

+0

[ネットワーク]タブで正しい応答が得られました。 – Mike

+0

要求のacceptsヘッダーを、応答すると予想されるファイルタイプに設定してみてください。 –

答えて

0
app.post('/', upload.any(), function(req, res, next) { 
    console.log(req.files); 
    var image = req.files; 
    console.log(image); 
    res.send(image); // this should come to jQuery response object 
    res.end(); // !!! - note this! 
}); 

応答を確定するには、res.end()を追加する必要があります。

関連する問題