Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the file name and the extension while uploading file in PhoneGap

I am uploding an image from a gallery using PhoneGap in Android but what I want to do is to fetch the file name and its extension which i am not be able to get it from imageuri so can any one tell me how can I find one

my imageURI is content://media/external/images/media/876 so is there a way to get a fileEntry by using this imageURI and read the file name and extension ?

function fileUpload(){

    navigator.camera.getPicture(
                uploadPhoto,
                function(message) { alert('get picture failed'); },
                {
                    quality         : 50,
                    destinationType : navigator.camera.DestinationType.FILE_URI,
                    sourceType      : navigator.camera.PictureSourceType.PHOTOLIBRARY
                }
            );


   }
    function uploadPhoto(imageURI) {
            var options = new FileUploadOptions();
            options.fileKey="uploaded_file";
            alert(imageURI);
            options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.mimeType="image/jpeg";

            var params = new Object();
            params.value1 = "test";
            params.value2 = "param";

            options.params = params;

            var ft = new FileTransfer();
            ft.upload(imageURI, encodeURI("http://www.mydomain.com/mobile_upload.php"), win, fail, options);
        }

        function win(r) {

            alert("WIN" +r.response);
            console.log("Code = " + r.responseCode);
            console.log("Response = " + r.response);
            console.log("Sent = " + r.bytesSent);
        }

        function fail(error) {

                    alert("error");

            alert("An error has occurred: Code = " + error.code);
            console.log("upload error source " + error.source);
            console.log("upload error target " + error.target);
        } 
like image 960
Hunt Avatar asked Oct 01 '22 09:10

Hunt


2 Answers

i found the answer and here is the code

window.resolveLocalFileSystemURI(imageURI, function(entry){

                console.log("****************HERE YOU WILL GET THE NAME AND OTHER PROPERTIES***********************");
                console.log(entry.name + " " +entry.fullPath);

            }, function(e){



            }); 
like image 66
Hunt Avatar answered Oct 05 '22 11:10

Hunt


I had the same problem and think that I found a solution. I think it is not the best, but possible ;-)

After get File_URI from camera resolve File system from File_URI and in this fileEntry get file. This file (here filee) is a variable called type, this is the mime type of file.

        function clickEvent() {

            navigator.camera.getPicture(cameraSuccess, cameraError, {
                destinationType: Camera.DestinationType.FILE_URI,
                sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM
            });
        }

        function cameraSuccess(file_URI) {
            window.resolveLocalFileSystemURI(file_URI, function(fileEntry) {
                fileEntry.file(function(filee) {
                    alert(filee.type); //THIS IS MIME TYPE
                }, function() {
                    alert('error');
                });

            }, onError);
        }

        function onError() {
            alert('fehler resolve file system');
        }

        function cameraError() {
            alert('fehler');
        }
like image 26
user3232739 Avatar answered Oct 05 '22 11:10

user3232739