【问题标题】:Cordova 'native file chooser' plugin for android is not working适用于 android 的 Cordova '本机文件选择器'插件不起作用
【发布时间】:2014-11-06 06:53:52
【问题描述】:

我必须在我的 phonegap 项目中实现文件上传功能。用户应该能够从手机内存或 SD 卡上传任何类型的文件。我试过输入 type="file",但在 android 4.4 中不支持。我也试过 phonegap camera API,但它只支持媒体文件。我发现 cordova 文件选择器插件 (https://github.com/cdibened/filechooser) 会有所帮助,但我无法让它工作。选择文件后不会立即触发成功回调函数。请在下面找到我的代码,

<input type="file" id="fileinput" name="fileinput"/>      

$("#fileinput").bind('click',function(){

       console.log("choose file selected");
       filechooser.open( {}, fileChooseSuccess, fileChooseFailed );
});


function fileChooseSuccess(data) {
        var filepath = data.filepath;
        console.log("file path:"+filepath);
}

function fileChooseFailed(msg) {
        console.log(msg);
}

当用户选择第二个文件时,第一个成功回调函数被触发。同样,当用户选择第三个文件(使用 Android 4.4.2 测试)时,会触发第二个成功回调函数。我无法解决此问题。 “filechooser.open”的第一个参数是强制性的吗?我必须通过什么来支持所有文件类型?

在插件文档中,它写道“您应该更改清单中的 com.ianhanniballake.localstorage.documents 以及 LocalStorageProvider.AUTHORITY 字段”。我必须改变什么?

非常感谢任何建议。

【问题讨论】:

    标签: cordova cordova-plugins


    【解决方案1】:

    phonegap/cordova 不支持文件的输入类型。对不起。

    您需要使用本示例中所示的 FileTransfer 插件,并在此处讨论How to upload file with Phonegap and JqueryMobile?

    function onDeviceReady() {
    
        // Retrieve image file location from specified source
        navigator.camera.getPicture(uploadPhoto,
                                    function(message) { alert('get picture failed'); },
                                    { quality: 50, 
                                    destinationType: navigator.camera.DestinationType.FILE_URI,
                                    sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY }
                                    );
    
    }
    
    function uploadPhoto(imageURI) {
        var options = new FileUploadOptions();
        options.fileKey="file";
        options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1)+'.png';
        options.mimeType="text/plain";
    
        var params = new Object();
    
        options.params = params;
    
        var ft = new FileTransfer();
        ft.upload(imageURI, encodeURI("http://some.server.com/upload.php"), win, fail, options);
    }
    
    function win(r) {
        console.log("Code = " + r.responseCode);
        console.log("Response = " + r.response);
        console.log("Sent = " + r.bytesSent);
    }
    
    function fail(error) {
        alert("An error has occurred: Code = " + error.code);
        console.log("upload error source " + error.source);
        console.log("upload error target " + error.target);
    }
    

    【讨论】:

    • 但是由于cordova的相机插件api只允许选择图片而不是其他文件类型,你将如何处理不是图像/视频的文件的场景?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    相关资源
    最近更新 更多