Skip to content

Instantly share code, notes, and snippets.

@rbtrsv
Forked from carzacc/UploadPageMultipartPOST.dart
Created September 27, 2022 08:41
Show Gist options
  • Save rbtrsv/fc77467821de8dbf0143b0bd768c9f2b to your computer and use it in GitHub Desktop.
Save rbtrsv/fc77467821de8dbf0143b0bd768c9f2b to your computer and use it in GitHub Desktop.
class UploadPage extends StatefulWidget {
UploadPage({Key key, this.url}) : super(key: key);
final String url;
@override
_UploadPageState createState() => _UploadPageState();
}
class _UploadPageState extends State<UploadPage> {
Future<String> uploadImage(filename, url) async {
var request = http.MultipartRequest('POST', Uri.parse(url));
request.files.add(await http.MultipartFile.fromPath('picture', filename));
var res = await request.send();
return res.reasonPhrase;
}
String state = "";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter File Upload Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(state)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
var file = await ImagePicker.pickImage(source: ImageSource.gallery);
var res = await uploadImage(file.path, widget.url);
setState(() {
state = res;
print(res);
});
},
child: Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment