I am trying to upload multiple files through a Flutter frontend, possibly to a Python server. I have not found any working code on how to upload files through Flutter Web. My frontend code is according an answer here: How to Pick files and Images for upload with flutter web
import 'package:http/http.dart' as http;
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
void main() {
/// your app lunch from here
runApp(new MaterialApp(
//// remove debug logo top left AppBar
debugShowCheckedModeBanner: false,
// application title
title: 'Hello World',
// whole content
home: TabsExample(),
));
}
class TabsExample extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return TabsState();
}
}
class TabsState extends State<TabsExample> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return DefaultTabController(
length: 1,
child: new Scaffold(
appBar: AppBar(
title: Text('Test Tab'),
bottom: TabBar(tabs: [
Tab(
icon: Text(
'Test',
),
),
]),
),
body: TabBarView(children: [
new FileUploadWithHttp(),
]),
));
}
}
class FileUploadWithHttp extends StatefulWidget {
@override
_FileUploadWithHttpState createState() => _FileUploadWithHttpState();
}
class _FileUploadWithHttpState extends State<FileUploadWithHttp> {
PlatformFile objFile;
PlatformFile result;
void chooseFileUsingFilePicker() async {
//-----pick file by file picker,
var result = await FilePicker.platform.pickFiles(
withReadStream:
true, // this will return PlatformFile object with read stream
allowMultiple: true);
print(result.files.length);
print(result.names);
// print(result.files.first.path); //not supported on web
if (result != null) {
setState(() {
objFile = result.files[0];
//print(objFile.readStream);
});
}
}
void uploadSelectedFile() async {
//---Create http package multipart request object
final request = http.MultipartRequest(
"POST",
Uri.parse("http://localhost:8000"), // e.g. localhost
);
//-----add other fields if needed
//request.fields["id"] = "abc";
//-----add selected file with request
request.files.add(new http.MultipartFile(
"file", objFile.readStream, objFile.size,
filename: objFile.name));
//-------Send request
var resp = await request.send();
//------Read response
String result = await resp.stream.bytesToString();
//-------Your response
print(result);
print('Upload successfull!');
}
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
//------Button to choose file using file picker plugin
ElevatedButton(
child: Text("Choose File"),
onPressed: () => chooseFileUsingFilePicker()),
//------Show file name when file is selected
if (objFile != null) Text("File name : ${objFile.name}"),
//------Show file size when file is selected
if (objFile != null) Text("File size : ${objFile.size} bytes"),
//------Show upload utton when file is selected
ElevatedButton(
child: Text("Upload"), onPressed: () => uploadSelectedFile()),
],
),
);
}
}
Running this on a python server according to this suggestion: https://gist.github.com/UniIsland/3346170
Or any other one that i've tried does not work, the server is not able to recieve the file properly. The error message is:
(False, "Can't find out file name...", 'by: ', ('::1', 62868, 0, 0))
Is there any straighforward way (possibly with code) on how to upload the file? Or do you have an idea why this error is coming? Any help would be greatly appreciated!