如何上传的PhoneGap和jQuery Mobile的任何类型的文件? [英] How to upload any type of file in phonegap and jquery mobile ?

查看:132
本文介绍了如何上传的PhoneGap和jQuery Mobile的任何类型的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建在其中,我想上传一个文件到服务器的PhoneGap的Andr​​oid的应用程序。我要的是当用户点击上传按钮的选项对话框在这里用户会选择一个文件打开上载,然后当保存按钮用户点击该文件将被上传。该对话框会像普通窗口一样,我们看到,当我们点击按钮附加在邮件中的桌面。
谁能告诉我如何做到这一点的Andr​​oid手机?
任何帮助是AP preciated。

I am creating an app in the phonegap android in which i want to upload a file to the server. What i want is when an user will click on the upload button an option dialog will open from where the user will select a file to be uploaded and then when user click on the save button the file will be uploaded. The dialog will be like normal window same as we see when we click on the attach button in the mail in the desktop. Can anyone tell me how to do this in android mobile? Any help is appreciated.

先谢谢了。

推荐答案

使用PhoneGap的文件传输方法
在文件传输的对象,您可以上传或下载文件,并从服务器。

Use phonegap file transfer method The FileTransfer object allows you to upload or download files to and from a server.

属性


  1. onprogress:与每当一个新的数据块被传送的ProgressEvent调用。 (功能)

  1. onprogress: Called with a ProgressEvent whenever a new chunk of data is transferred. (Function)

方法


  • 上传:发送一个文件到服务器

  • upload: sends a file to a server.

下载:下载从服务器的文件

download: downloads a file from server.

中止:中止正在进行的传输

abort: Aborts an in-progress transfer.

样品
//!假设变量fileURI所包含一个有效的URI到一个文本文件中的设备上

Sample // !! Assumes variable fileURI contains a valid URI to a text file on the device

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  });

了解更多信息检查链接

这篇关于如何上传的PhoneGap和jQuery Mobile的任何类型的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆