1

Using PhoneGap is it possible to get the mime type of a file in the local file system?

Part of my app downloads a file from a remote URL on our server and saves it to the Android downloads folder. I then use the WebIntent plugin to open the file but one of the options this requires is a mime type.

I've not been able to find anything in API Documentation or Google other than here, but that is more relevant to forms.

Has anyone tried to do this before?

Greg
  • 157
  • 1
  • 11

2 Answers2

3

Something like this would work on Android but YMMV on other platforms:

var medFile = new MediaFile("empty.txt", "file:///mnt/sdcard/empty.txt");
console.log("file path = " + medFile.fullPath);
medFile.getFormatData(function(metadata) {
    console.log("mimeType = " + metadata.type);
}, function() {
    console.log("fail");
});

metadata.type would be your mime type.

Simon MacDonald
  • 23,205
  • 5
  • 56
  • 73
1
    function getType(file_URI) {
        window.resolveLocalFileSystemURI(file_URI, function(fileEntry) {
            fileEntry.file(function(filee) {
                alert(filee.type); //THIS IS MIME TYPE
            }, function() {
                alert('error getting MIME type');
            });

        }, onError);
    }
Alex from Jitbit
  • 39,345
  • 13
  • 111
  • 109
  • 1
    `resolveLocalFileSystemURL` doesnot for content URI like this `content://com.android.providers.media.documents/document/image%3A2119` . If you have any work around to find out the mime type of file using above url please suggest me. – Saravanan Jul 27 '15 at 23:39
  • Subscribing to above comment, on iOS the `filee.type` exists but is `null` – Ionut May 23 '17 at 14:39