2016-09-13 96 views
3

我試圖將我從Cordova(cordova plugin file transfer)獲取的圖片上傳到nodeJS服務器。 這是我的移動應用的代碼:將cordova文件傳輸插件中的文件上傳到nodeJS服務器

function uploadToServer(pictureName, fileURI) { 

    var options = new FileUploadOptions(); 
    options.fileKey = "file"; 
    options.mimeType = "image/jpeg"; 
    options.fileName = pictureName; 

    var ft = new FileTransfer(); 
    ft.upload(fileURI, encodeURI(CONSTANTS.hosts.remoteSR), 
    function (res) { 
     console.log("Code = " + res.responseCode); 
    }, 
    function (error) { 
     $log.debug(error) 
     alert("An error has occurred: Code = " + error.code); 
    }, 
    options); 

} 

PS:了fileURI且pictureName是的Valide PARAMS和withinother功能正確地進行測試。

我的節點JS服務器代碼:

var express = require("express"); 
var multer = require('multer'); 
var app   = express(); 

var storage = multer.diskStorage({ 
    destination: function (req, file, callback) { 
    callback(null, './uploads'); 
    }, 
    filename: function (req, file, callback) { 
    callback(null, file.fieldname + '-' + Date.now()); 
    } 
}); 
var upload = multer({ storage : storage}).single('userPhoto'); 

app.get('/',function(req,res){ 
    res.sendFile(__dirname + "/index.html"); 
}); 

app.post('/api/photo',function(req,res){ 
    upload(req,res,function(err) { 
    if(err) { 
     console.log(err) 
     return res.end("Error uploading file."); 
    } 

    res.end("File is uploaded"); 
    }); 
}); 

app.listen(3000,function(){ 
    console.log("Working on port 3000"); 
}); 

PS:上傳工作正常,當我從其他來源上傳。 (例如index.html上傳表格)

當我執行代碼時會發生什麼: 「代碼= 200」,這意味着上傳成功,但不知何故,我沒有找到我的文件上傳。

問:如何正確地插科爾多瓦文件transfert pllugin與

的NodeJS

節點版本:4.4.7 科爾多瓦版本:6.x的

回答

5

好,我發現:

options.fileKey = "file"; 

應匹配

multer({ storage : storage}).single('userPhoto'); 

so options.fileKey should be s et對此用戶照片

options.fileKey = "userPhoto"; 
+0

這幫了我。非常感謝您分享您的答案。 – Somename

+0

非常感謝您的建議,它解決了我的問題。我試圖從移動端發送wav文件到nodejs,以便最近在nodejs服務器上傳文件時丟失了.wav ext,我在以下方法中添加了 callback(null,file.fieldname +' - '+ Date.now()+ '的.wav'); –

相關問題