在PhoneGap中上传文件时获取文件名和扩展名

时间:2014-03-07 19:13:46

标签: android cordova

我在Android中使用 PhoneGap 从图库中升级图像,但我想要的是获取文件名及其扩展名,我无法从获取imageuri 所以任何人都可以告诉我如何找到一个

我的imageURIcontent://media/external/images/media/876所以有没有办法通过使用此imageURI来获取fileEntry并读取文件名和扩展名?

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);
        } 

2 个答案:

答案 0 :(得分:2)

我找到了答案,这里是代码

window.resolveLocalFileSystemURI(imageURI, function(entry){

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

            }, function(e){



            }); 

答案 1 :(得分:2)

我遇到了同样的问题,并认为我找到了解决方案。我认为这不是最好的,但可能; - )

从相机获取File_URI后,从File_URI和此fileEntry获取文件中解析文件系统。这个文件(这里是filee)是一个名为type的变量,这是mime类型的文件。

        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');
        }