Skip to content

Instantly share code, notes, and snippets.

@sankarcheppali
Created January 25, 2020 10:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sankarcheppali/6e477ad3ec96cc29aec8d4eab5fe1558 to your computer and use it in GitHub Desktop.
Save sankarcheppali/6e477ad3ec96cc29aec8d4eab5fe1558 to your computer and use it in GitHub Desktop.
flutter image upload
import 'package:flutter/material.dart';
import 'package:path/path.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'dart:async';
import 'package:dio/dio.dart';
/**
* accepts three parameters, the endpoint, formdata (except fiels),files (key,File)
* returns Response from server
*/
Future<Response> sendForm(
String url, Map<String, dynamic> data, Map<String, File> files) async {
Map<String, MultipartFile> fileMap = {};
for (MapEntry fileEntry in files.entries) {
File file = fileEntry.value;
String fileName = basename(file.path);
fileMap[fileEntry.key] =
MultipartFile(file.openRead(), await file.length(), filename: fileName);
}
data.addAll(fileMap);
var formData = FormData.fromMap(data);
Dio dio = new Dio();
return await dio.post(url,
data: formData, options: Options(contentType: 'multipart/form-data'));
}
/**
* accepts two parameters,the endpoint and the file
* returns Response from server
*/
Future<Response> sendFile(String url, File file) async {
Dio dio = new Dio();
var len = await file.length();
var response = await dio.post(url,
data: file.openRead(),
options: Options(headers: {
Headers.contentLengthHeader: len,
} // set content-length
));
return response;
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
File _image;
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.camera);
print('upload started');
//upload image
//scenario one - upload image as poart of formdata
var res1 = await sendForm('http://192.168.43.236:4082/create-profile',
{'name': 'iciruit', 'des': 'description'}, {'profile': image});
print("res-1 $res1");
var res2 =
await sendFile('http://192.168.43.236:4082/profile-pic-upload', image);
print("res-2 $res2");
setState(() {
_image = image;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Image Picker Example'),
),
body: Center(
child: _image == null ? Text('No image selected.') : Image.file(_image),
),
floatingActionButton: FloatingActionButton(
onPressed: getImage,
tooltip: 'Pick Image',
child: Icon(Icons.add_a_photo),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment