使用Drupal服务模块将图像从phonegap应用程序上传到Drupal 7

时间:2014-03-31 07:33:49

标签: php drupal cordova drupal-7 drupal-services

我想使用Drupal服务模块将图像从Phonegap应用程序上传到Drupal 7后端。 为了测试目的,我已经用核心php完成了它,并且每件事都正常工作但我需要使用Drupal服务模块做同样的事情。

我正在进行注册过程,用户可以从移动设备上传图像作为个人资料图片,我需要在drupal 7后端访问注册数据和图像。

核心PHP: -

<?php 
    // Directory where uploaded images are saved
    $dirname = "uploads/"; 

    // If uploading file
    if ($_FILES) {
        print_r($_FILES);
        mkdir ($dirname, 0777, true);
        move_uploaded_file($_FILES["file"]["tmp_name"],$dirname."/".$_FILES["file"]["name"]);
    }
?>

Phonegap Code

function uploadFile() {
    // Get URI of picture to upload
    navigator.camera.getPicture( function(uri) {
        try {
            var img = document.getElementById('pimage');
            img.style.visibility = "visible";
            img.style.display = "block";
            var imageURI = uri;
            if (!imageURI || (img.style.display == "none")) {
                document.getElementById('picture_msg').innerHTML = "Tap on picture to select image from gallery.";
                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('picture_msg').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded.";
                    img.src = uri;
                    img.width = 100;
                    img.height = 100;
                },
                function(error) {
                    document.getElementById('picture_msg').innerHTML = "Upload failed: Code = "+error.code;
                }, options);
            }
        }
        catch(exce) {
            alert(exce);
        }
    },
    function(e) {
        console.log("Error getting picture: " + e);
        document.getElementById('bio_description').innerHTML = "Error getting picture.";
    },
    {
        quality: 50,
        destinationType: navigator.camera.DestinationType.FILE_URI,
        sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
    }
);}

0 个答案:

没有答案