multipartfile upload in flutter web

4,314 views
Skip to first unread message

andrea antonello

unread,
Oct 3, 2019, 8:54:50 AM10/3/19
to flutt...@googlegroups.com
Hello all,
I am trying to upload a file using flutter web.

Looking around the net I found I should do that via multipartfile of
the html package.
But dart:html doesn't contain a MultipartFile class.

Can anyone redirect me right in this?

Thanks,
Andrea

Andy Greenshaw

unread,
Oct 3, 2019, 9:28:33 AM10/3/19
to andrea antonello, flutt...@googlegroups.com
Take a look at the dio package.

Cheers,
Andy
 

From: flutt...@googlegroups.com on behalf of andrea antonello <andrea.a...@gmail.com>
Sent: Thursday, October 3, 2019 1:54 pm
To: flutt...@googlegroups.com
Subject: multipartfile upload in flutter web
 
--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/CACck5jO3ggV%2BRjab1xwXnUXUKMbMiRxoAw-8GM5N%2B72cui%3Dnqw%40mail.gmail.com.

andrea antonello

unread,
Oct 3, 2019, 9:31:39 AM10/3/19
to flutt...@googlegroups.com
> Take a look at the dio package.

I am using it for flutter android/ios, but I thought (I have to look
where I saw that on the dio page) it would not work for flutter for
the web.
Just to make sure: you confirm it works for flutter web?

Thanks a ton,
Andrea

andrea antonello

unread,
Oct 3, 2019, 9:33:46 AM10/3/19
to flutt...@googlegroups.com
Sorry for the double-post, I found it once I had sent already.

As the issue tracker states, dio is not supported on flutter web:
https://github.com/flutterchina/dio/issues/313

They suggest to use the official html package but I haven't been able to do so.

Cheers,
Andrea

andrea antonello

unread,
Oct 4, 2019, 7:25:42 AM10/4/19
to flutt...@googlegroups.com
Fond the solution. Just as information for anyone else interested,
dart:html has FileUploadInputElement, which can be called from onTap
and works in flutter web:

onTap: () async {
InputElement uploadInput = FileUploadInputElement();
uploadInput.click();

uploadInput.onChange.listen((e) {
final files = uploadInput.files;
if (files.length == 1) {
final file = files[0];
final reader = new FileReader();
reader.onLoadEnd.listen((e) {
print("loaded: ${file.name}");
print("type: ${reader.result.runtimeType}");
});
reader.onError.listen((e) {
print(e);
});
reader.readAsArrayBuffer(file);
}
});
},


Credits go toi:
https://stackoverflow.com/questions/56457214/is-there-any-plugin-or-way-to-upload-file-to-server-using-flutter-web

Cheers,
Andrea





On Thu, Oct 3, 2019 at 3:33 PM andrea antonello

vinay sharma

unread,
Dec 28, 2019, 8:12:13 PM12/28/19
to Flutter Development (flutter-dev)
sir, after reading file as arrayBuffer how to proceed to upload it on server?


Travis Dixon

unread,
Dec 28, 2019, 9:14:32 PM12/28/19
to Flutter Development (flutter-dev)
I was literally just reading this thread looking for the same :P


Note though that it appears to freeze animations while large files upload, so CircularProgressIndicator is not a good spinner for this case
Message has been deleted

vinay sharma

unread,
Dec 28, 2019, 10:40:44 PM12/28/19
to Flutter Development (flutter-dev)

ty for replying sir ;P

i already saw that implementation but the problem is how should i convert the File \
that i read as ArrayBuffer to List<int> (or Uint8List to use that image in Flutter  widget) ;(
?

Travis Dixon

unread,
Dec 28, 2019, 10:57:11 PM12/28/19
to Flutter Development (flutter-dev)
If you're using android studio (maybe works for vscode too I don't know) you can control click on reader.result and you'll see that if it's got a Buffer it converts it to a specialized form of Uint8List.
If it's got a string (eg, if you called the function that reads as text) it returns a string.

Here's my (fairly clunky) file picker widget with the relevant line highlighted, feel free to re-use any/all/none as you will:

vinay sharma

unread,
Dec 28, 2019, 11:25:17 PM12/28/19
to Flutter Development (flutter-dev)
wow! ty sir ;P

Actually i ws doing same thing but only problem ws i wsnt  changing my state variables in setState inside ;( 
ty for helping ;P

Travis Dixon

unread,
Dec 28, 2019, 11:39:44 PM12/28/19
to Flutter Development (flutter-dev)
Maybe I should tidy it up, convert it to a plugin and publish it on pub.dev
Reply all
Reply to author
Forward
0 new messages