簡體   English   中英

文件上傳按鈕在Cordova / Phonegap項目中仍然不起作用

[英]File upload button is still not working in Cordova / Phonegap Project

我無法使用PHP后端將圖片上傳到Web服務器。 我的cordova相機腳本能夠拍攝照片並以小尺寸顯示照片。 但是它不能上傳圖像。 我不知道為什么。 我調用函數photoUpload(); 並在按鈕中設置一個onClick-event

<button class="camera-control" onclick="photoUpload();">UPLOAD</button>

這是我的JavaScript,怎么了?

var pictureSource;
var destinationType;

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    pictureSource = navigator.camera.PictureSourceType;
    destinationType = navigator.camera.DestinationType;
}

function clearCache() {
    navigator.camera.cleanup();
}

var retries = 0;
function onCapturePhoto(fileURI) {
    $("#cameraPic").attr("src", fileURI);
    var win = function (r) {
        clearCache();
        retries = 0;
        navigator.notification.alert(
        '',
        onCapturePhoto,
        'Der Upload wurde abgeschlossen',
        'OK');
        console.log(r);
    }

    var fail = function (error) {
        navigator.notification.alert(
        'Bitte versuchen Sie es noch einmal.',
        onCapturePhoto,
        'Ein unerwarteter Fehler ist aufgetreten',
        'OK');
        console.log("upload error source " + error.source);
        console.log("upload error target " + error.target);
        if (retries == 0) {
            retries ++
            setTimeout(function() {
                onCapturePhoto(fileURI)
            }, 1000)
        } else {
            retries = 0;
            clearCache();
            alert('Fehler!');
        }
    }

    function photoUpload() {
    var fileURI = $("#cameraPic").attr("src");
    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
    options.mimeType = "image/jpeg";
    options.chunkedMode = false;

    var params = new Object();
    params.fileKey = "file";
    options.params = {}; // eig = params, if we need to send parameters to the server request


    var ft = new FileTransfer();
    ft.upload(fileURI, encodeURI("http://xxxx/app/upload.php"), win, fail, options);
    }

}


function capturePhoto() {
    navigator.camera.getPicture(onCapturePhoto, onFail, {
    quality: 50,
    destinationType: destinationType.FILE_URI
    });
}


function getPhoto(source) {
      navigator.camera.getPicture(onPhotoURISuccess, onFail, {
      quality: 50,
      destinationType: destinationType.FILE_URI,
      sourceType: source });
    }

function onFail(message) {
    alert('Failed because: ' + message);
}

看一下您的功能photoUpload位於功能onCapturePhoto中! 您需要在頂層移動photoUpload功能。

window.photoUpload = function() {
var fileURI = $("#cameraPic").attr("src");
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
options.chunkedMode = false;

var params = new Object();
params.fileKey = "file";
options.params = {}; // eig = params, if we need to send parameters to the server request


var ft = new FileTransfer();
ft.upload(fileURI, encodeURI("http://xxxx/app/upload.php"), win, fail, options);
}

更好的方法是:

<button class="camera-control" id="photoUploadButton;">UPLOAD</button>

document.getElementById("photoUploadButton").addEventListener("click", photoUpload);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM