我正在phonegap android中创建一个应用程序,我想在其中将文件上传到服务器。我想要的是当用户单击上传按钮时,将打开一个选项对话框,用户将在其中选择要上传的文件,然后当用户单击保存按钮时,文件将被上传。当我们单击桌面邮件中的附加按钮时,该对话框将与我们看到的普通窗口一样。谁能告诉我如何在安卓手机中做到这一点?任何帮助表示赞赏。
提前致谢。
我正在phonegap android中创建一个应用程序,我想在其中将文件上传到服务器。我想要的是当用户单击上传按钮时,将打开一个选项对话框,用户将在其中选择要上传的文件,然后当用户单击保存按钮时,文件将被上传。当我们单击桌面邮件中的附加按钮时,该对话框将与我们看到的普通窗口一样。谁能告诉我如何在安卓手机中做到这一点?任何帮助表示赞赏。
提前致谢。
使用 phonegap 文件传输方法 FileTransfer 对象允许您将文件上传到服务器或从服务器下载文件。
特性
onprogress:每当传输新的数据块时使用 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 });
更多信息检查链接