Back

[SOLVED] Upload image flutter web, ios and andoid.

  • 0
  • Flutter
  • Storage
plushteddy
4 Oct, 2023, 06:20

Hello,

How can I pick an image, and then upload it to appwrite without saving it?

TL;DR
TL;DR: The user had an issue with uploading an image to Appwrite using Flutter for web, iOS, and Android. They received an error about not providing file bytes for Flutter web. The solution was to use `InputFile.fromBytes` instead of `fromPath` in Flutter web. The user also had a question about whether they needed to save the image before uploading it, to which others clarified that the image is already saved in the gallery and does not need to be saved again.
Maniac_Fighter
4 Oct, 2023, 06:38

https://pub.dev/packages/image_picker Is this something you are looking for?

plushteddy
4 Oct, 2023, 06:55

yes, but i have to save the imahe ig?

Maniac_Fighter
4 Oct, 2023, 06:57

I didn't understand what do you mean by saving ? This package picks the image from your gallery

plushteddy
4 Oct, 2023, 06:59

yes, but when i want to upload it to appwrite i have to pass the path and for that I have to save it right?

Maniac_Fighter
4 Oct, 2023, 07:02

save what actually? If i am right the image is already in the gallery right?

plushteddy
4 Oct, 2023, 07:05

ah bruh

plushteddy
4 Oct, 2023, 07:13

did like that but got an error:

TypeScript
Future _getImage() async {
    final ImagePicker picker = ImagePicker();
    final XFile? image = await picker.pickImage(source: ImageSource.gallery);

    final storage = Storage(db());

    final file = await storage.createFile(
      bucketId: '650fe518eed83eab7514',
      fileId: ID.unique(),
      file: InputFile.fromPath(path: image!.path.toString(), filename: User.name().toString() + '.jpg'),
    );```

  }
plushteddy
4 Oct, 2023, 07:13

Error: AppwriteException: , File bytes must be provided for Flutter web (0) dart-sdk/lib/internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:49 throw packages/appwrite/src/client_browser.dart 129:7 chunkedUpload dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5 _async packages/appwrite/src/client_browser.dart 119:33 chunkedUpload packages/appwrite/services/storage.dart 74:30 createFile dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5 _async packages/appwrite/services/storage.dart 52:33 createFile packages/main/views/screens/test.dart 28:31 _getImage dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50 <fn> dart-sdk/lib/async/zone.dart 1661:54 runUnary dart-sdk/lib/async/future_impl.dart 156:18 handleValue dart-sdk/lib/async/future_impl.dart 840:44 handleValueCallback dart-sdk/lib/async/future_impl.dart 869:13 _propagateToListeners dart-sdk/lib/async/future_impl.dart 641:5 [_completeWithValue] dart-sdk/lib/async/future_impl.dart 715:7 callback dart-sdk/lib/async/schedule_microtask.dart 40:11 _microtaskLoop dart-sdk/lib/async/schedule_microtask.dart 49:5 _startMicrotaskLoop dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 181:15 <fn>

Maniac_Fighter
4 Oct, 2023, 07:17

seems like you cannot pass the path from Flutter Web use this instead https://pub.dev/documentation/appwrite/latest/appwrite/InputFile/InputFile.fromBytes.html In this way you are passing bytes instead of path XFile also has a property of getting the byte data for that file as well

Maniac_Fighter
4 Oct, 2023, 07:19

https://appwrite.io/docs/products/storage/upload-download#create-file It's also mentioned in the docs to use .fromBytes instead of fromPath in Flutter Web

plushteddy
4 Oct, 2023, 07:20

like that?

TypeScript
Future _getImage() async {
    final ImagePicker picker = ImagePicker();
    final XFile? image = await picker.pickImage(source: ImageSource.gallery);

    final storage = Storage(db());

    final Uint8List bytes = image?.readAsBytes() as Uint8List;

    final file = await storage.createFile(
      bucketId: '650fe518eed83eab7514',
      fileId: ID.unique(),
      file: InputFile.fromBytes(bytes: bytes, filename: "image.jpg"),

    );
  }```
Maniac_Fighter
4 Oct, 2023, 07:21

yess, but AFAIK readAsBytes() is an async Function so you might wanna await here

plushteddy
4 Oct, 2023, 07:23

works thx

plushteddy
4 Oct, 2023, 07:24

can you also tell me, how i can show an image from the storage without downloading it?

Drake
5 Oct, 2023, 16:36

most of the time, you should not call toString() to solve the problem of something requiring a string. by doing that, you're probably incorrectly coercing the input

Drake
5 Oct, 2023, 16:37

plesae make sure to format stuff like this as multiline code so it's easier to read

Drake
5 Oct, 2023, 16:38
Drake
5 Oct, 2023, 16:38

[SOLVED] Upload image flutter web, ios and andoid.

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more