【问题标题】:Image not uploaded using cordova file transfer android图片未使用cordova文件传输android上传
【发布时间】:2014-05-28 21:17:50
【问题描述】:

我创建了一个页面来拍摄图像或从手机图库中选择图像并且工作正常,但我想将这张选择的照片上传到我在 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 中,这样就可以了。 选择上传此图片的第二个按钮 注意:会显示 alert('large')。

【问题讨论】:

    标签: javascript android cordova


    【解决方案1】:

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

    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"]);}
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 2016-05-14
      • 2013-12-12
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      相关资源
      最近更新 更多