Skip to content

Instantly share code, notes, and snippets.

@carzacc
Created February 2, 2020 17:58
Show Gist options
  • Save carzacc/b592ac6630b6158bcaefd955b9a50509 to your computer and use it in GitHub Desktop.
Save carzacc/b592ac6630b6158bcaefd955b9a50509 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