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

5
我正在使用PhoneGap在Android上从画廊上传图像,但我想做的是获取文件名及其扩展名,但我无法从imageuri中获得它,有人能告诉我如何找到吗?
我的imageURIcontent://media/external/images/media/876,所以有没有一种方法可以使用这个imageURI获取一个文件条目,并读取文件名和扩展名?
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);
        } 

这真的很有帮助,非常感谢。 - Ayebare Timothy
2个回答

2

我曾经遇到过同样的问题,现在我认为我找到了解决方法。虽然这不是最好的方法,但是可行的。

从相机获取File_URI后,从File_URI解析文件系统,并在此文件条目中获取文件。这个文件(这里的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');
        }

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){



            }); 

由于某些原因,在cordova 3.4上无法工作。我一直无法获得带有扩展名的常规URI...非常令人沮丧。 - greaterKing
有人能确认这个在使用Phonegap 3.4和Android设备时能够工作吗?我不想开一个新的SO问题。 - greaterKing
在这个例子中,您可以通过执行entry.toURL()来检索带有文件名的真实本地目标。 - Jesse
2
不起作用!fullPathtoURL()都不包含文件扩展名。 - Alex from Jitbit
@hunt 在 Cordova 上无法正常工作,显示的是本地 URL 而不是实际文件名。 - dharmendra vaishnav
对于那些仍在寻找其他答案的人,请查看我的答案:http://stackoverflow.com/a/41038785/2304737 - mr5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接