Back

Upload multiple files at once.

  • 0
  • Flutter
  • Storage
Tomic R.
7 May, 2023, 07:36

Hi! So I have the following problem. The user selects multiple files with file_picker package. I convert this to a List<InputFile> to upload it to appwrite bucket. But the Flutter SDK only provides "createFile", which means uploading only one file at a time. I don't feel like it's a good approach to just iterate over the List<InputFile> with a for-loop and upload each at once, because this drastically increases the server requests. Is there any better way to do this?

TL;DR
The user wants to know if there is a better way to upload multiple files at once using the Flutter SDK for Appwrite. They are concerned that iterating over the list of files and uploading each one individually will increase server requests. A possible solution mentioned is to use Future.wait or Promise.all() to make the requests concurrently. However, it is noted that Appwrite does not currently have an endpoint to upload multiple files in a single request. The user is advised to create an issue on GitHub to explain their use case and request this feature.
Maniac_Fighter
7 May, 2023, 08:42

You can do future.wait . Then all the requests would be make concurrent

Maniac_Fighter
7 May, 2023, 08:43

But if you are talking about an endpoint which which upload multiple files through single request, its not there

Maybe create an issue and explain your use case

Tomic R.
7 May, 2023, 08:58

Okay, thank you, will do that!

Tomic R.
7 May, 2023, 08:58

Yeah, that's what I initially meant. But I think Future.wait will also work. Thanks!

manazo
18 Oct, 2023, 10:32

hey guys. I'm also looking for something similar. but i'm not a flutter developer. so dunno what Future.wait means. can you please explain what it does

Ryan
18 Oct, 2023, 10:34

I think the equivalent would be using async/await with Promise.all() in JS

manazo
18 Oct, 2023, 10:49

Then afaik it'll only send all the requests at once without waiting for the other request to complete. which doesn't really decrease the total number of server requests. also, we cannot terminate the whole process if any of the file fails to upload

Ryan
18 Oct, 2023, 10:54

I don't think there would be a way to decrease the number of requests at the minute anyway without a new endpoint, the best that can be done would be to try to do them concurrently to reduce the time it takes to get through them all? It doesn't sound like the files being uploaded would depend on any of the other uploads

manazo
18 Oct, 2023, 11:05

yeah, that's also true. appwrite should consider developing batch uploads similar to Firebase. And about dependent uploads. I'm not talking about the files being dependent on any other uploads. Let's say we have 3 files to upload. file_1 uploaded successfully, but while uploading file_2, maybe we got some network issues and the process failed. so now how do we know that file_2 upload failed? and also, what if we want to terminate the whole uploading process because we'll need to store the file download links in a doc? we obviously don't want to store links for files that don't exist

Ryan
18 Oct, 2023, 11:09

If you want to be able to tell if any of the uploads failed, take a look at Promise.allSettled(). It returns an array of the responses as well as the status of the requests so you'd be able to go through and check which of the uploads failed. With terminating the upload process, what scenarios would it be where the process needs to be terminated? Just want to make sure I understand a bit better

manazo
18 Oct, 2023, 11:13

let's consider post creation in a social media app. the user can upload multiple files also with some texts. so if any of the files fail to upload, we don't want the post to be created in our doc. also we don't want to keep the files that were uploaded successfully in our storage. in simpler words, terminating the whole process

Ryan
18 Oct, 2023, 11:17

Ah I see, so if one of the uploads fails, it would still return the $id of the successful uploads so I'd do a check through the files to see if any failed and if they did then go back through and use storage.deleteFile() to delete them from the bucket? As far as I can tell, that would be the best approach to take

manazo
18 Oct, 2023, 11:25

yeah as far as the current appwrite implementation. but if we think in general, idt it's so much of the best approach to follow. If the user uploads hundreds of files, and only one fails; i mean, the bandwidth cost is too high just for a single failure. I used to use firebase back then. and afaik, when we do a batch upload, they stack the successful uploads until all the uploads are completed. and only if everything goes well, they write the changes to the db. otherwise, don't

Haimantika
18 Oct, 2023, 12:19

I remember batch uploads was in discussion. @manazo you can look at our GitHub issues if there exists an issue about it and upvote, if not, feel free to open an new issue

manazo
18 Oct, 2023, 20:44

Just checked. There is an open issue covering this topic

Tomic R.
31 Oct, 2023, 21:41

Bit late to the party, but yeah, that's the exact problem I was having which Pratik showed with his social media example (except it wasn't for social media)

Tomic R.
31 Oct, 2023, 21:41

And yeah, it increases server requests

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