【问题标题】:How to get mimeType from Cordova File Transfer Plugin?如何从 Cordova 文件传输插件获取 mimeType?
【发布时间】:2015-12-15 05:38:57
【问题描述】:

我正在开发混合移动应用程序。

在一种情况下,我们需要在选择或上传文件时从文件中获取 mimeType。

我正在使用 apache FileTransfer。

window.resolveLocalFileSystemURL(fileURI , resolveOnSuccess, resolveOnFail)

【问题讨论】:

    标签: javascript cordova mime-types file-transfer apache-cordova


    【解决方案1】:

    您可以从 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
    });
    

    【讨论】:

    • 你确定这行得通吗?我传入了如下的 uri:content://com.android.providers.media.documents/document/video%3A818,但它仍然没有返回任何内容。我每次都收到FileError
    【解决方案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;
            });        
        }
    });
    

    【讨论】:

      【解决方案3】:

      file-transfer 不公开 mimeType 和其他 FileUploadOptions 参数。

      Windows plugin code 中的上传支持 Mimetype 自动检测。

      还有here is a Jira ticket for this feature CB-5946——它也有一些关于Android实现的建议。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 对于最新版本的 Ionic 和 Cordova,文件类型没有类型参数。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-21
        • 2011-03-27
        • 2018-07-19
        • 2018-01-11
        • 1970-01-01
        • 2018-02-17
        相关资源
        最近更新 更多