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

时间:2018-12-11 03:42:12

标签: firebase nativescript angular2-nativescript

我正在尝试评估使用nativescript-imagepicker上传的图片的哑剧类型和大小。但是,似乎模块本身无法做到这一点。如何将约束应用于正在上传的文件? (例如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);
        });
}

1 个答案:

答案 0 :(得分:1)

您将必须阅读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");
}