简体   繁体   English

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

[英]upload file from cordova file transfer plugin to nodeJS server

I am trying to upload a picture I take from cordova ( cordova plugin file transfer ) to nodeJS server.我正在尝试将我从cordova( cordova插件文件传输)拍摄的图片上传到nodeJS服务器。 Here is my mobile app code :这是我的移动应用程序代码:

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 and pictureName are valide params and tested withinother functions correctly. PS:fileURI 和pictureName 是valide 参数并在其他函数中正确测试。

My node JS server Code :我的节点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 : the upload is working fine when i upload from other sources. PS:当我从其他来源上传时,上传工作正常。 (from for example the index.html upload form) (例如来自 index.html 上传表单)

What happens when I execute the code : "Code= 200" which means upload success but somehow I don't find my file uploaded.当我执行代码时会发生什么:“代码= 200”这意味着上传成功但不知何故我没有找到我的文件上传。

Question : how to plug correctly cordova file transfert pllugin with nodeJS问题:如何使用nodeJS正确插入cordova文件传输插件

Node version : 4.4.7 Cordova Version : 6.x节点版本:4.4.7 Cordova 版本:6.x

ok I found it :好的,我找到了:

options.fileKey = "file";  

should match应该匹配

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

so options.fileKey should be set to userPhoto like this所以 options.fileKey 应该像这样设置为 userPhoto

options.fileKey = "userPhoto";  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM