Skip to content

Instantly share code, notes, and snippets.

@siteslave
Created March 27, 2021 02:29
Show Gist options
  • Save siteslave/36cc2e4813f819704dd9efeb0f66d562 to your computer and use it in GitHub Desktop.
Save siteslave/36cc2e4813f819704dd9efeb0f66d562 to your computer and use it in GitHub Desktop.
///
Future uploadFile(File imageFile, int employeeId) async {
try {
EasyLoading.show(status: 'อัปโหลดไฟล์...');
String token = await storage.read(key: "token");
await api.uploadImage(employeeId, imageFile, token);
EasyLoading.showSuccess('อัปโหลดสำเร็จ');
getEmployees();
} catch (error) {
EasyLoading.showError('เกิดข้อผิดพลาด');
print(error);
}
}
///
IconSlideAction(
caption: 'อัปโหลด',
color: Colors.teal,
icon: Icons.camera,
onTap: () async {
final pickedFile = await picker.getImage(
source: ImageSource.gallery,
imageQuality: 50,
maxHeight: 680,
maxWidth: 680);
if (pickedFile != null) {
File imageFile = File(pickedFile.path);
int employeeId =
int.parse(emp['employee_id'].toString());
uploadFile(imageFile, employeeId);
}
},
),
///
var _rnd = new Random();
setState(() {
rnd = _rnd.nextInt(10000);
_token = token;
});
///
CircleAvatar(
backgroundColor: Colors.grey[100],
child: Image.network(
'https://5b32a5b16b98.ngrok.io/libs/image/profile/${emp['employee_id']}?$rnd',
headers: {"Authorization": "Bearer $_token"},
),
)
///
Future<Response> uploadImage(
int employeeId, File imageFile, String token) async {
String path = '/employees/$employeeId/upload';
FormData formData = FormData.fromMap({
"file": await MultipartFile.fromFile(imageFile.path, filename: basename(imageFile.path))
});
return await dio.post(path,
data: formData,
options: Options(
headers: {HttpHeaders.authorizationHeader: 'Bearer $token'}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment