Name of picture uploaded with file phonegap plugin “image%A3456”

杀马特。学长 韩版系。学妹 提交于 2019-11-28 14:44:05

Filename setting in options object seems to be the issue. Setting proper filename resolves the issue.

Here the code that work:

    function uploadImage() {
    document.getElementById('picture_msg').innerHTML = "";
    // Get URI of picture to upload
    navigator.camera.getPicture(
       // function(uri) {
        function(imageURI) {
             window.FilePath.resolveNativePath(imageURI, function(result) {
            try {
                // Pick image from div
                 var img = document.getElementById('pimage');

                img.style.visibility = "visible";
                img.style.display = "block";
                var imageURI=result;
                //imageURI = document.getElementById('pimage').getAttribute("src");
                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('server').value;

                console.log("Server "+server);
                if (server) {
                    // Specify transfer options
                    var options = new FileUploadOptions();
                    options.fileKey="file";
                                        alert(imageURI);
                   options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
                   //alert(imageURI.substr(imageURI.lastIndexOf('/')+1)+'.png');
                    options.mimeType="image/jpeg";


                    //options.mimeType=mimeType;
                    options.chunkedMode = false;
                    options.headers = {
                         Connection: "close"
                    };

                    // Transfer picture to server
                    var ft = new FileTransfer();
                    ft.upload(imageURI,server, function(r) {

                         //window.FilePath.resolveNativePath(imageURI, function(result) {
                        document.getElementById('picture_msg').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded.";
                             /* window.FilePath.resolveNativePath(imageURI, function(result) {
                              document.getElementById('picture_msg').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded."; */
                             // onSuccess code
                             //$('#image-upload img').attr('src')
                             //imageURI = document.getElementById('pimage').getAttribute("src");
                              //imageURI = result;
                              //uri = result;
                             //alert(uri);
                        img.src = imageURI;
                        img.width = 100;
                        img.height = 100;
                         // });            
                        //alert(uri);
                    },
                function(error) {
                        document.getElementById('picture_msg').innerHTML = "Upload failed: Code = "+error.code;
                    }, options);
                }
                else {
                    document.getElementById('picture_msg').innerHTML = "Server Not Found";
                }
            }
            catch(exce) {
                alert(exce);
            }
        });
        },
        function(e) {
            console.log("Error getting picture: " + e);
            document.getElementById('picture_msg').innerHTML = "No Image Found";
        },
        {
            quality: 50,
            destinationType: navigator.camera.DestinationType.FILE_URI,
            sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
        }
    );
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!