简体   繁体   English

如何从Cordova文件传输插件获取mimeType?

[英]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. 在一种情况下,当我们选择或上传文件时,我们需要从文件中获取mimeType。

I am using apache FileTransfer. 我正在使用apache FileTransfer。

window.resolveLocalFileSystemURL(fileURI , resolveOnSuccess, resolveOnFail) window.resolveLocalFileSystemURL(fileURI,resolveOnSuccess,resolveOnFail)

you can get it from cordova File plugin. 您可以从cordova File插件获取它。

$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: 我在TypeScript和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. file-transfer不会公开mimeType和其他FileUploadOptions参数。

Mimetype autodetection is only supported for uploads in Windows plugin code . Mimetype自动检测仅支持Windows插件代码中的上载。

And here is a Jira ticket for this feature CB-5946 - it also has some suggestions on Android implementation. 这是此功能CB-5946的Jira票证 -它还提供了有关Android实现的一些建议。

In Angular 2 I use this: 在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)));
      });
    }
  });
});  };

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. resolveLocalFileSystemURL通过成功回调返回一个Entry ,我必须将该Entry转换为FileEntry才能访问该文件方法,该方法将返回一个File ,该File扩展了具有mime type属性的Blob

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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