【问题标题】:upload file from cordova file transfer plugin to nodeJS server从cordova文件传输插件上传文件到nodeJS服务器
【发布时间】:2017-01-21 01:55:59
【问题描述】:

我正在尝试将我从 cordova (cordova plugin file transfer) 拍摄的图片上传到 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 是有效参数,并在其他函数中正确测试。

我的节点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 上传表单)

当我执行代码时会发生什么: "Code= 200" 表示上传成功,但不知何故我找不到上传的文件。

问题:如何用nodeJS正确插入cordova文件传输插件

节点版本:4.4.7 科尔多瓦版本:6.x

【问题讨论】:

    标签: javascript node.js cordova android-camera


    【解决方案1】:

    好的,我找到了:

    options.fileKey = "file";  
    

    应该匹配

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

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

    options.fileKey = "userPhoto";  
    

    【讨论】:

    • 这对我有帮助。非常感谢您分享您的答案。
    • 非常感谢您的建议,它解决了我的问题。我试图将wav文件从移动设备发送到nodejs,所以在nodejs服务器中上传的新文件丢失了.wav ext,我通过以下方式添加了回调(null,file.fieldname +'-'+ Date.now()+'。 wav');
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 2015-11-21
    相关资源
    最近更新 更多