我正在 phonegap android 中创建一个应用程序,我想在其中将文件上传到服务器。我想要的是当用户单击上传按钮时,将打开一个选项对话框,用户将从中选择要上传的文件,然后当用户单击保存按钮时,文件将被上传。该对话框将与我们在桌面上单击邮件中的附加按钮时看到的普通窗口一样。 谁能告诉我如何在 android 手机中执行此操作? 感谢您的帮助。
提前致谢。
最佳答案
使用phonegap文件传输方式 FileTransfer 对象允许您将文件上传到服务器或从服务器下载文件。
属性
onprogress:每当传输新的数据 block 时调用 ProgressEvent。 (函数)
方法
上传:将文件发送到服务器。
下载:从服务器下载文件。
abort:中止正在进行的传输。
样本 //!!假设变量 fileURI 包含设备上文本文件的有效 URI
var win = function (r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}
var fail = function (error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
options.mimeType = "text/plain";
var params = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
ft.upload(fileURI, encodeURI("http://some.server.com/upload.php"), win, fail, options);
您可以使用
浏览和选择文件var source = navigator.camera.PictureSourceType.PHOTOLIBRARY;
navigator.camera.getPicture(successFn, errorFn, { quality: 50,
destinationType: this.photoDestinationType.FILE_URI,
sourceType: source,
mediaType: navigator.camera.MediaType.ALLMEDIA });
更多信息查看link
关于android - 如何在 phonegap 和 jquery mobile 中上传任何类型的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18485883/