【问题标题】:How to upload any type of file in phonegap and jquery mobile ?如何在 phonegap 和 jquery mobile 中上传任何类型的文件?
【发布时间】:2013-08-31 09:46:51
【问题描述】:

我正在 phonegap android 中创建一个应用程序,我想在其中将文件上传到服务器。我想要的是当用户单击上传按钮时,将打开一个选项对话框,用户将在其中选择要上传的文件,然后当用户单击保存按钮时,文件将被上传。当我们单击桌面邮件中的附加按钮时,该对话框将与我们看到的普通窗口一样。 谁能告诉我如何在安卓手机中做到这一点? 任何帮助表示赞赏。

提前致谢。

【问题讨论】:

    标签: android jquery-mobile file-upload cordova


    【解决方案1】:

    使用phonegap文件传输方式 FileTransfer 对象允许您从服务器上传或下载文件。

    属性

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

    更多信息查看link

    【讨论】:

    • 如果想上传 .pdf,.png,.jpg,.txt 等文件我应该怎么做 bcz 这取决于用户他/她想要上传什么样的文件所以我想要的是它会打开一个SD卡,然后用户将选择用户要上传的内容。
    • 在fileURI中如何设置目录的路径来选择一个文件,如果我们使用navigator.camera,那么用户将只能选择图像文件或者他们将能够选择任何扩展名要上传的文件很抱歉问你这样的问题我是新手。如果你能给出一个完整的例子,我会很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    相关资源
    最近更新 更多