Cordova 在一个HTTP请求中上载多个文件
是否有任何方法可以使用cordova在一个HTTP请求中上载多个文件。我发现所有的解决方案都对每个图像上传使用单独的HTTP调用。我问了Raymond camden同样的问题。他用这个很棒的博客回复了我 我只是简单地使用相机插件,让你从设备上选择多个图像。每个图像都被添加到DOM中。这是代码:Cordova 在一个HTTP请求中上载多个文件,cordova,phonegap-plugins,cordova-plugins,ngcordova,Cordova,Phonegap Plugins,Cordova Plugins,Ngcordova,是否有任何方法可以使用cordova在一个HTTP请求中上载多个文件。我发现所有的解决方案都对每个图像上传使用单独的HTTP调用。我问了Raymond camden同样的问题。他用这个很棒的博客回复了我 我只是简单地使用相机插件,让你从设备上选择多个图像。每个图像都被添加到DOM中。这是代码: var images = []; var $imagesDiv; document.addEventListener("deviceready", init, false); function init
var images = [];
var $imagesDiv;
document.addEventListener("deviceready", init, false);
function init() {
$("#addPicture").on("touchend", selPic);
$imagesDiv = $("#images");
$("#uploadPictures").on("touchend", uploadPics);
}
function selPic() {
navigator.camera.getPicture(function(f) {
var newHtml = "<img src='"+f+"'>";
$imagesDiv.append(newHtml);
images.push(f);
if(images.length === 1) {
$("#uploadPictures").removeAttr("disabled");
}
}, function(e) {
alert("Error, check console.");
console.dir(e);
}, {
quality: 50,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
destinationType: Camera.DestinationType.FILE_URI
});
}
所以从上到下,我所做的是创建一个promise对象数组。或者更具体地说,是它们的jQuery版本。然后,我为用户选择的每个图像运行一个上载调用。我检查服务器的结果,并根据服务器所说的内容,用0或1解决承诺
最后,我调用了$。来处理等待所有这些异步进程完成的问题。实际上我没有显示任何内容,我只是console.dir,但您可以想象检查结果并做——很好——任何有意义的事情
我希望这对大家有用,一如既往,如果你有任何问题,请告诉我。您可以在此处找到完整的源代码:
参考资料:在我这边,我正在尝试将所有图像/文件压缩到一个文件中,然后上载单个压缩文件。
这个潜在解决方案的问题是:为android和ios找到一个cordova zip函数,但在这里,每个图像上传都会调用单独的HTTP调用,对吗?我说的是在一个HTTP调用中上传所有图像唯一的选择是使用多个输入类型的文件来选择文件,但在一些android版本上不起作用
function uploadPics() {
console.log("Ok, going to upload "+images.length+" images.");
var defs = [];
images.forEach(function(i) {
console.log('processing '+i);
var def = $.Deferred();
function win(r) {
console.log("thing done");
if($.trim(r.response) === "0") {
console.log("this one failed");
def.resolve(0);
} else {
console.log("this one passed");
def.resolve(1);
}
}
function fail(error) {
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
def.resolve(0);
}
var uri = encodeURI("http://localhost/testingzone/test.cfm");
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=i.substr(i.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
var ft = new FileTransfer();
ft.upload(i, uri, win, fail, options);
defs.push(def.promise());
});
$.when.apply($, defs).then(function() {
console.log("all things done");
console.dir(arguments);
});
}