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で空白の応答がありません。私はこれを解決する方法がわかりません。
[ネットワーク]タブをチェックして、実際に応答として得たものを確認しましたか? jQueryはレスポンスを解析しようとします。そのため、レスポンスは空になります。 –
[ネットワーク]タブで正しい応答が得られました。 – Mike
要求のacceptsヘッダーを、応答すると予想されるファイルタイプに設定してみてください。 –