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

get selected file size, from chooser plugin Cordova/Ionic

我正在尝试 select 使用 Cordova/Ionic5 中的 Chooser 插件的文件。

我可以获取文件和 dataURI,但是如何获取 selected 文件大小。

请帮忙。

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

构造函数

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