How to get mimeType from Cordova file transfer module?

I am developing a hybrid mobile application.

In one of the scenarios, we need to extract mimeType from the file when selecting or loading the file.

I am using apache FileTransfer.

window.resolveLocalFileSystemURL (file URI, resolveOnSuccess, resolveOnFail)

+4
source share
4 answers

You can get it from File plugin cordons.

$cordovaFile.checkFile(uri, '')
.then(function(entry) {
    // success
    var name = entry.name;

    entry.file(function(data) {
        // get mime type
        var mime = data.type;
        alert(mime);
    })

}, function(error) {
    // error
    // show toast
});
+2
source

file-transfer does not disclose mimeType parameters and other FileUploadOptions parameters.

Mimetype Auto Detection is only supported for downloads to Windows plugin code .

Jira CB-5946 - Android.

0

Angular 2 :

export class Plugins {

albums = {
    open () : Promise<any>  {
        return ImagePicker.getPictures({
                quality: 100,
                maximumImagesCount: 1,
        }).then((imgUrls) => {
            return imgUrls;
        }, (err) => {
            if(err.error == "cordova_not_available") {
                alert("Cordova is not available, please make sure you have your app deployed on a simulator or device");
            } else {
                console.log("Failed to open albums: " + err.error);
            }
        });
    },
}

...

@Component({
  templateUrl: 'build/pages/home/home.html',
  directives: [UploadButton]
})
export class HomePage implements OnInit {

openAlbums = (): void => {
var $self = this;
this._plugins.albums.open().then((imgUrls) => {
  imgUrls.forEach((imageUrl: string): void => {
    if (imageUrl) {
      window.resolveLocalFileSystemURL(imageUrl, function (entry: FileEntry) {
        entry.file(file=> {
          console.log('mimeType', file.type);
        }, ((error:FileError) => console.log(error)));
      });
    }
  });
});  };

resolveLocalFileSystemURLreturns the success callback EntryI had to pass in FileEntryto access the file method, which returns File, which extends Blob, which has a property of type mime.

0
source

I worked like in TypeScript and Angular 2:

this._File.resolveLocalFilesystemUrl(somefileUri).then((entry: Entry) => {
    if (entry) {
        var fileEntry = entry as FileEntry;
        fileEntry.file(success => { 
            var mimeType = success.type;
        }, error => {
            // no mime type found;
        });        
    }
});
0
source

Source: https://habr.com/ru/post/1620036/


All Articles