Cordova-2.1.0捕获并上传照片到服务器

时间:2012-10-22 00:19:50

标签: android cordova cordova-2.0.0

我是新来的,上周有问题:

我在phonegap(cordova2.1.0)中创建了一个应用程序,这是代码:

<script type="text/javascript" charset="utf-8">

var deviceReady = false;

/**
 * Take picture with camera
 */
function takePicture() {
    navigator.camera.getPicture(
        function(uri) {
            var img = document.getElementById('camera_image');
            img.style.visibility = "visible";
            img.style.display = "block";
            img.src = uri;
            document.getElementById('camera_status').innerHTML = "Success";
        },
        function(e) {
            console.log("Error getting picture: " + e);
            document.getElementById('camera_status').innerHTML = "Error getting picture.";
        },
        { quality: 50, destinationType: navigator.camera.DestinationType.FILE_URI});
};

/**
 * Select picture from library
 */
function selectPicture() {
    navigator.camera.getPicture(
        function(uri) {
            var img = document.getElementById('camera_image');
            img.style.visibility = "visible";
            img.style.display = "block";
            img.src = uri;
            document.getElementById('camera_status').innerHTML = "Success";
        },
        function(e) {
            console.log("Error getting picture: " + e);
            document.getElementById('camera_status').innerHTML = "Error getting picture.";
        },
        { quality: 50, destinationType: navigator.camera.DestinationType.FILE_URI, sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY});
};

/**
 * Upload current picture
 */
function uploadPicture() {

    // Get URI of picture to upload
    var img = document.getElementById('camera_image');
    var imageURI = img.src;
    if (!imageURI || (img.style.display == "none")) {
        document.getElementById('camera_status').innerHTML = "Take picture or select picture from library first.";
        return;
    }

    // Verify server has been entered
    server = document.getElementById('serverUrl').value;
    if (server) {

        // Specify transfer options
        var options = new FileUploadOptions();
        options.fileKey="file";
        options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
        options.mimeType="image/jpeg";
        options.chunkedMode = false;

        // Transfer picture to server
        var ft = new FileTransfer();
        ft.upload(imageURI, server, function(r) {
            document.getElementById('camera_status').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded.";              
        }, function(error) {
            document.getElementById('camera_status').innerHTML = "Upload failed: Code = "+error.code;               
        }, options);
    }
}

/**
 * View pictures uploaded to the server
 */
function viewUploadedPictures() {

    // Get server URL
    server = document.getElementById('serverUrl').value;
    if (server) {

        // Get HTML that lists all pictures on server using XHR 
        var xmlhttp = new XMLHttpRequest();

        // Callback function when XMLHttpRequest is ready
        xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState === 4){

                // HTML is returned, which has pictures to display
                if (xmlhttp.status === 200) {
                    document.getElementById('server_images').innerHTML = xmlhttp.responseText;
                }

                // If error
                else {
                    document.getElementById('server_images').innerHTML = "Error retrieving pictures from server.";
                }
            }
        };
        xmlhttp.open("GET", server , true);
        xmlhttp.send();         
    }   
}

/**
 * Function called when page has finished loading.
 */
function init() {
    document.addEventListener("deviceready", function() {deviceReady = true;}, false);
    window.setTimeout(function() {
        if (!deviceReady) {
            alert("Error: PhoneGap did not initialize.  Demo will not run correctly.");
        }
    },2000);
}

HTML code:

图片:                        

    <input type="button" onclick="takePicture();" value="Take Picture" /><br/>
    <input type="button" onclick="selectPicture();" value="Select Picture from Library" /><br/>
    <input type="button" onclick="uploadPicture();" value="Upload Picture" />
</div>
<br/>
好吧:这段代码的一部分不是我的。

问题

没有错误提醒

警报说一切都已完成。图片上传和字节

在我的msql中,图片没有扩展名(.jpg),只有名称

并且在images文件夹中没有上传

我将所有内容列入白名单,

anibody可以帮帮我吗? 我不知道要做其他人......

提前致谢

顺便说一下,在eclipse日志中,everithing也表现得很好, 在android 2.3实际手机中同样的事情,但图像不存在..

0 个答案:

没有答案