【发布时间】:2017-06-23 20:25:22
【问题描述】:
我目前正在开发一个 Phonegap 应用程序,我希望用户能够将任何文件上传到我的 NodeJS 服务器。
我浏览了整个网络,但我什么都做不了......
这是我用于 Phonegap 控制器的代码:
$scope.open = function()
{
navigator.camera.getPicture(upload,
function(message)
{
alert('get picture failed');
},
{
quality: 50,
destinationType: navigator.camera.PictureSourceType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY,
mediaType: navigator.camera.MediaType.ALLMEDIA
});
}
var win = function (r) {
$scope.log = "Code = " + r.responseCode;
$scope.log2 = "Response = " + r.response;
$scope.log3 = "Sent = " + r.bytesSent;
$scope.$digest();
}
var fail = function (error) {
$scope.log = "An error has occurred: Code = " + error.code;
$scope.log2 = "upload error source " + error.source;
$scope.log3 = "upload error target " + error.target;
$scope.$digest();
}
function upload(fileURI)
{
$scope.log = fileURI;
$scope.$digest();
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
options.mimeType = "text/plain";
options.chunkedMode = false;
var params = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
ft.upload(fileURI, "http://192.168.192.111:2999/upload", win, fail, options);
}.
这是NodeJS服务器的当前代码,尝试了很多不同的东西,都没有成功:
var express = require('express');
var http = require('http').Server(express);
var io = require('socket.io')(http);
var fs = require('fs');
var multer = require('multer');
var app = new express();
app.post('/upload', multer({dest: './uploads/'}).single('upl'), function(req, res)
{
console.log(req.body);
console.log(req.file);
})
http.listen(2999, function(){
console.log('listening on *:2999');
});
在应用程序中,我曾经收到 FileUploadOptions 等未定义的错误,但我通过将它们添加到 cordova 项目来解决这个问题。 此外,如果可以帮助任何人,我会使用 ionic 1。
我确实不断收到错误代码 1(上传错误源),即使我选择了一个真实文件并且我看到链接是正确的(在我的 Android 设备上类似于 /storage/0/emulated/Downloads)。
另外,有时它也会给我错误 3(上传目标源),我认为是某种找不到服务器的问题。
有什么明显的我做错了,我该如何解决?有没有更方便的方法,因为我最终想将它链接到 MySQL 数据库。
提前致谢!
【问题讨论】:
标签: node.js cordova file-transfer