简体   繁体   中英

How to get mimeType from Cordova File Transfer Plugin?

I am developing hybrid mobile application.

In one of the scenario we need to fetch mimeType from a file when we select or upload a file.

I am using apache FileTransfer.

window.resolveLocalFileSystemURL(fileURI , resolveOnSuccess, resolveOnFail)

you can get it from cordova File plugin.

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

I got it working like this 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;
        });        
    }
});

file-transfer does not expose mimeType and other FileUploadOptions params.

Mimetype autodetection is only supported for uploads in Windows plugin code .

And here is a Jira ticket for this feature CB-5946 - it also has some suggestions on Android implementation.

In Angular 2 I use this:

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

resolveLocalFileSystemURL gives back through the success callback an Entry which I had to cast to FileEntry to get access to the file method which gives back a File which extends Blob that has the mime type property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM