图像未使用cordova文件传输android上传

时间:2014-05-28 21:17:50

标签: javascript android cordova

我创建了一个页面来拍摄图像或从手机图库中选择图像并正常工作,但我想将这张照片上传到Godaddy上我的服务器。 我使用Cordova文件传输上传,通过命令行安装文件传输:

 cordova plugin add https://github.com/apache/cordova-plugin-file-transfer.git

我放了一个小代码来上传这张照片,但没有留言提醒(没有错误也没有成功)。

选择图像的代码:

    function onPhotoURISuccess(imageURI) {
            // Uncomment to view the image file URI
            // console.log(imageURI);

            // Get image handle
            //
            var largeImage = document.getElementById('largeImage');

            // Unhide image elements
            //
            largeImage.style.display = 'block';

            // Show the captured photo
            // The in-line CSS rules are used to resize the image
            //
            largeImage.src = imageURI;

            upload();
        }

代码上传功能:

 function upload() {
            alert('large');
            var uploadingImage = document.getElementById('largeImage');
            var imgUrl = uploadingImage.src;
            window.resolveLocalFileSystemURI(imgUrl, resolveOnSuccess, fsFail);
            options = new FileUploadOptions();
            // parameter name of file:
            options.fileKey = "my_image";
            // name of the file:
            options.fileName = imgUrl.substr(imgUrl.lastIndexOf('/') + 1);

            // mime type:
            options.mimeType = "image/jpeg";
            params = {val1: "some value", val2: "some other value"};
            options.params = params;
            ft = new FileTransfer();
            ft.upload(fileuri, "http://siencelb.org/raycoding/insurance/avatar", success, fail, options);
        }
 function resolveOnSuccess(entry) {
            fileuri = entry.toURL();
            //use fileuri to upload image on server
        }

        function fsFail(message) {
            alert("Error Message: " + message + "Error Code:" + message.target.error.code);
        }

我先选择两个按钮来选择图像并将其放入div largeImage中,这样可行。 第二个按钮,用于上传此图像 注意:会显示警告('大')。

1 个答案:

答案 0 :(得分:0)

我解决了我的错误,我想发布它

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({quality: 50, destinationType: navigator.camera.DestinationType.FILE_URI, sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY});
        }
        ;


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 = "upload.php";
            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);
            }
        }

upload.php的PHP代码

<?php
// Directory where uploaded images are saved
$dirname = "/avatar/"; 
// If uploading file
if ($_FILES) {  
print_r($_FILES);  
mkdir ($dirname, 0777, true);  
move_uploaded_file($_FILES["file"]["tmp_name"],$dirname."/".$_FILES["file"]["name"]);}
?>