【问题标题】:Phonegap/Cordova upload image filePhonegap/Cordova 上传图片文件
【发布时间】:2014-11-16 03:06:30
【问题描述】:

在外部网站中有一个输入(类型 - 文件)字段,我需要将图像上传到此输入。 在 android 中,我使用 WebChromeClient 和 openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) 函数完成了这项任务。 我不知道如何在 phonegap / cordova 中解决这个任务。

我的代码是:

function initialize() {
    document.addEventListener('deviceready', onDeviceReady, false);
    var ref = null;
}
function onDeviceReady() {

    ref = window.open('http://example.com',
            '_blank', 'location=no', 'EnableViewPortScale=yes');
    ref.addEventListener('loadstop', LoadStop);

}

function LoadStop(event) {
    if (event.url == 'http://example.com/Add') {
        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) {
    console.log("imageURI----: " + imageURI);

    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = "IMAG0475";
    options.mimeType = "image/*";

    var params = new Object();
    options.params = params;

    var ft = new FileTransfer();
    ft.upload(imageURI,
            encodeURI("http://example/Add"), 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);
}

【问题讨论】:

标签: image file cordova file-upload


【解决方案1】:

默认情况下,cordova 应用程序无法访问 Android 手机中的所有存储位置,例如 sdcard、外部文件。

要解决此问题,请将以下行复制到您的 cordova 项目的 config.xml 中,

<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,root"/>

Config.xml 可以在 /platforms/android/res/xml/config.xml 中找到

只需添加以上行并再次构建项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    相关资源
    最近更新 更多