2016-02-28 28 views
0

我正在使用Node的JS,Formidable和Cloudinary依賴關係讓用戶將圖像上傳到Cloudinary數據庫。節點JS,強大的,Cloudinary圖像上傳

用戶將從多部分表單上傳圖像,我希望這些字段成爲傳遞給Cloudinary上傳函數的變量。

我的代碼有效,但我無法將表單信息作爲Cloudinary參數傳遞。

這裏是我當前的代碼,這將原始文件名:

router.post('/upload', function (req, res){ 
var form = new formidable.IncomingForm(); 
form.parse(req, function(err, fields, files) { 
    res.writeHead(200, {'content-type': 'text/plain'}); 
    res.write('received upload:\n\n'); 
    res.end(util.inspect({fields: fields, files: files})); 
}); 

form.on('end', function(fields, files) { 
    var temp_path = this.openedFiles[0].path; 
    var file_name = this.openedFiles[0].name; 

    cloudinary.uploader.upload(temp_path, function(result) { console.log(result) }, 
     { public_id: file_name }); 
}) 

但我不希望原始文件名。取而代之的var file_name我想var image_namewhich is provided by the user from the form

回答

0

順便說一句,有沒有需要處理end,因爲你提供了一個回調parse()。回調在end被觸發時被調用。

用戶輸入的文字將在fields中。