【问题标题】:PhoneGap to php image upload works every other timePhoneGap到php图像上传每隔一段时间工作一次
【发布时间】:2014-04-09 20:09:28
【问题描述】:

我发现,如果您在服务器上使用带有 php 的 phonegap 上传图像,它每隔一段时间就会起作用。始终如一。第一个upolad成功,第二个失败,第三个成功,第四个失败,依此类推。

我正在使用位于此处的示例: How to retrieve POST data from PhoneGaps File Transfer API

我正在使用安卓手机进行测试。

// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready

function onDeviceReady() {
    console.log("device ready");
    // Do cool things here...
}

function getImage() {
    // 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 captureImage() {
    // 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
    });
}

function uploadPhoto(imageURI) {
    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
    options.mimeType = "image/jpeg";
    var params = new Object();
    params.value1 = "test";
    params.value2 = "param";
    options.params = params;
    options.chunkedMode = false;
    var ft = new FileTransfer();
    ft.upload(imageURI, "http://someserver.com/somedir/up.php", win, fail, options);
}

function win(r) {
    console.log("Code = " + r.responseCode.toString() + "\n");
    console.log("Response = " + r.response.toString() + "\n");
    console.log("Sent = " + r.bytesSent.toString() + "\n");
    alert("Code Slayer!!!");
}

function fail(error) {
    alert("An error has occurred: Code = " + error.code);
}

还有php

<?php
print_r($_FILES);
$new_image_name = "YEAH.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "../uploads/".$new_image_name);
?>

【问题讨论】:

  • 请发布您的代码。

标签: php file cordova upload


【解决方案1】:

这是一个比较常见的问题,但很容易绕过。当上传失败时,只需重试几次,直到成功。

uploadPhoto function(imageURI) {
    ft.upload(filePath, yourUrl, function(response) {
                    console.log("SUCCESS")
                }, function(e) {
                    console.log("## FAIL")
                    if(uploadRetry < 4){
                        uploadRetry++;
                        uploadPhoto(imageURI);
                    } else {
                        //HANDLE FAIL AFTER IT FAILS TOO MANY TIMES
                    }
                }, options, true);
}

它通常在第二次尝试时起作用。

【讨论】:

    【解决方案2】:

    试试这个

    var pictureSource; // picture source
    var destinationType; // sets the format of returned value
     // Wait for device API libraries to load
     //
    document.addEventListener("deviceready", onDeviceReady, false);
    // device APIs are available
     //
    
    function onDeviceReady() {
        pictureSource = navigator.camera.PictureSourceType;
        destinationType = navigator.camera.DestinationType;
    }
    // Called when a photo is successfully retrieved
     //
    
    function onPhotoDataSuccess(imageURI) {
        // Uncomment to view the base64-encoded image data
        // console.log(imageData);
        // Get image handle
        //
        var smallImage = document.getElementById('image');
        // Unhide image elements
        //
        smallImage.style.display = 'block';
        // Show the captured photo
        // The inline CSS rules are used to resize the image
        //
        smallImage.src = imageURI;
        upload(imageURI);
    }
    // Called when a photo is successfully retrieved
     //
    
    function onPhotoURISuccess(imageURI) {
        // Uncomment to view the image file URI
        // console.log(imageURI);
        // Get image handle
        //
        var largeImage = document.getElementById('image');
        // Unhide image elements
        //
        largeImage.style.display = 'block';
        // Show the captured photo
        // The inline CSS rules are used to resize the image
        //
        largeImage.src = imageURI;
        upload(imageURI);
    }
    // A button will call this function
     //
    
    function capturePhoto() {
        // Take picture using device camera and retrieve image as base64-encoded string
        navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
            quality: 30,
            targetWidth: 600,
            targetHeight: 600,
            destinationType: destinationType.FILE_URI,
            saveToPhotoAlbum: true
        });
    }
    // A button will call this function
     //
    
    function capturePhotoEdit() {
        // Take picture using device camera, allow edit, and retrieve image as base64-encoded string
        navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
            quality: 30,
            targetWidth: 600,
            targetHeight: 600,
            allowEdit: true,
            destinationType: destinationType.FILE_URI
        });
    }
    // A button will call this function
     //
    
    function getPhoto(source) {
        // Retrieve image file location from specified source
        navigator.camera.getPicture(onPhotoURISuccess, onFail, {
            quality: 30,
            targetWidth: 600,
            targetHeight: 600,
            destinationType: destinationType.FILE_URI,
            sourceType: source
        });
    }
    // Called if something bad happens.
     //
    
    function onFail(message) {
        alert('Failed because: ' + message);
    }
    
    function upload(imageURI) {
        var options = new FileUploadOptions();
        options.fileKey = "file";
        options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
        options.mimeType = "image/jpeg";
        var params = new Object();
        params.param1 = "param 1";
        params.param2 = "param 2";
        options.params = params;
        options.chunkedMode = false;
        var ft = new FileTransfer();
        ft.upload(imageURI, "https://example.com/upload.php", win, fail, options);
    
        function win(r) {
            console.log("Code = " + r.responseCode);
            console.log("Response = " + r.response);
            console.log("Sent = " + r.bytesSent);
            alert(r.response);
        }
    
        function fail(error) {
            alert("An error has occurred: Code = " + error.code);
        }
    }
    

    【讨论】:

    • 我得到了同样的结果:每隔一段时间就成功。
    猜你喜欢
    • 2015-10-13
    • 2021-08-16
    • 2010-10-23
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2017-11-17
    相关资源
    最近更新 更多