我无法获取要使用 nativescript 上传的文件的大小

I'm unable to get the size of a file to upload with nativescript

我正在尝试评估使用 nativescript-imagepicker 上传的图像的 mime 类型和大小。但是似乎模块本身无法做到这一点。如何将约束应用于我正在上传的文件? (如 max sizejust png or jpg

这是我的代码:

 const context = imagepicker.create({ mode: "single" });
    context
        .authorize()
        .then(() => {
            console.log('imagePicker.authorize...');
            return context.present();
        })
        .then((selection) => {
            if (!selection || !selection.forEach) {
                console.log('Error on selection empty or not array:', selection);
                return;
            }
            selection.forEach((selected) => {
                this.processPhoto(selected);
            });
        }).catch((err) => {
         .
         .
         .

正在处理图像...

    processPhoto (selectedPhoto: any) {
    console.log('uploading photo to firebase', selectedPhoto);
    this.firebaseService.getImagePickerLocalFilePath(selectedPhoto)
        .then((localFilePath: string) => {
            console.log('about to upload file:', localFilePath);
            return this.firebaseService.uploadFile(localFilePath);
        })
        .catch((err) => {
            this.isLoading = false;
            this.messageService.handleErrorRes(err);
        });
}

您必须阅读 File 上的 sizeextension,并在它们不符合您的限制条件时阻止上传。

const file = fileSystemModule.fromPath(localFilePath);
if (file.size <= YOUR_SIZE_LIMIT_IN_BYTES && file.extension.toLowerCase() === "png") {
    // Upload
} else {
   alert("File can't be uploaded");
}