简体   繁体   English

在使用 firebase、flutter 和图像裁剪器将文件上传到 firebase 之前重命名文件

[英]renaming file before uploading it to firebase using firebase, flutter and image cropper

Good Day.再会。 I would like to ask on how do you rename your file(image) before uploading it in your firebase storage?我想问一下在上传到 firebase 存储之前如何重命名文件(图像)?

I am using image_cropper when selecting and cropping my images.我在选择和裁剪图像时使用image_cropper However I cannot see if it has any built-in function for renaming files.但是我看不到它是否有任何内置的 function 用于重命名文件。

This is my selectFile when selecting images in the local device.这是我在本地设备中选择图像时的 selectFile。

Future selectFile() async {
    try {
      final image = await ImagePicker().pickImage(source: ImageSource.gallery);
      if (image != null) {
        final croppedImage = await ImageCropper().cropImage(
          sourcePath: image.path,
          aspectRatio: const CropAspectRatio(ratioX: 1, ratioY: 1),
          compressQuality: 100,
          maxWidth: 450,
          maxHeight: 450,
          compressFormat: ImageCompressFormat.jpg,
          aspectRatioPresets: [CropAspectRatioPreset.square],
        );
        setState(() {
          imagePath = croppedImage!.path;
        });
      }
    } on PlatformException catch (e) {
      print(e);
      Navigator.of(context).pop();
    }
  }

and this is my uploadFile function这是我的上传文件 function

Future uploadFile() async {
    final path = 'products/${imagePath.split("/").last}';
    
    final file = File(imagePath);

    final ref = FirebaseStorage.instance.ref().child(path);
    uploadTask = ref.putFile(file);

    final snapshot = await uploadTask!.whenComplete(() {});

    final urlDownload = await snapshot.ref.getDownloadURL();
    var url = urlDownload.toString();
    print(url);

the imagePath is my selected image. imagePath是我选择的图像。

and every time I upload it on my firebase storage it renames itself as image_cropper_1234567890.jpg and I just want to display the image as 1234567890每次我将它上传到我的 firebase 存储时,它都会将自己重命名为image_cropper_1234567890.jpg我只想将图像显示为1234567890

Try this:尝试这个:

final path = 'products/${imagePath.split("_").last}';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM