将文件从cordova文件传输插件上传到nodeJS服务器

Med*_*umy 3 javascript node.js android-camera cordova

我正在尝试将我从cordova(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);

}
Run Code Online (Sandbox Code Playgroud)

PS:fileURI和pictureName是valide params,并在其他函数中正确测试.

我的节点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");
});
Run Code Online (Sandbox Code Playgroud)

PS:当我从其他来源上传时,上传工作正常.(例如index.html上传表单)

当我执行代码时会发生什么:"Code = 200"这意味着上传成功,但不知何故我没有找到我上传的文件.

问题:如何使用nodeJS正确插入cordova文件transfert pllugin

节点版本:4.4.7 Cordova版本:6.x

Med*_*umy 5

好的我找到了:

options.fileKey = "file";  
Run Code Online (Sandbox Code Playgroud)

应该匹配

multer({ storage : storage}).single('userPhoto');
Run Code Online (Sandbox Code Playgroud)

所以options.fileKey应该像这样设置为userPhoto

options.fileKey = "userPhoto";  
Run Code Online (Sandbox Code Playgroud)