代码之家  ›  专栏  ›  技术社区  ›  raju

从选择器插件Cordova/Ionic获取选定的文件大小

  •  0
  • raju  · 技术社区  · 4 年前

    我正在尝试使用以下命令选择文件 Chooser Cordova/Ionic5中的插件。

    我能够获取文件和数据URI,但如何获取所选的文件大小。

    请帮忙。

    0 回复  |  直到 4 年前
        1
  •  4
  •   Marco Taylor Rahul    4 年前

    文件选择器插件用于文件选择。。它不会为您提供文件信息,如大小mimetype等。您需要一些其他插件,如 file filepath 从文件中获取此信息。我在下面提到了这些链接,并添加了我的示例代码,请查看如何通过这些插件获取这些信息。

    构造器

    import { FileChooser } from '@ionic-native/file-chooser/ngx';
    import { FilePath } from '@ionic-native/file-path/ngx';
    import { File } from '@ionic-native/file/ngx';
    
    constructor(
            private fileChooser: FileChooser,
            private filepath : FilePath,
            private file : File,
            private plt : Platform
        
        ) { }
    

    文件选择功能

    async selectFile(){
        
        if(this.plt.is('android')){
    const selectedFile : string = await this.fileChooser.open();
            const resolvedPath = await this.filepath.resolveNativePath(selectedFile);
            const fileEntry = await this.file.resolveLocalFilesystemUrl(resolvedPath) as any;
            fileEntry.file((fileInfo)=>{
                console.log('File Info ', fileInfo);
                //sample response file mime type size etc you can use it as per your requirement
                // end: 3028
                // lastModified: 1610953273000
                // lastModifiedDate: 1610953273000
                // localURL: "cdvfile://localhost/sdcard/Download/sample.pdf"
                // name: "sample.pdf"
                // size: 3028
                // start: 0
                // type: "application/pdf"
            })
        }else{
            //For ios device 
            const selectedFile : string = await  this.filePicker.pickFile() ;
            const fileEntry = await this.file.resolveLocalFilesystemUrl('file:///'+selectedFile) as any;
            fileEntry.file((fileInfo)=>{
                 console.log('File Info ', fileInfo);
            })
        }
    }
    

    https://ionicframework.com/docs/native/file

    https://ionicframework.com/docs/native/file-path