upload image in flutter from assets code example
Example 1: uploading image flutter
var d = "$tmpFile";
var dd = d.split("'");
var filename = dd[1].split('/').last;
FormData formData = new FormData.fromMap({
"user_id": "3225",
"image": await MultipartFile.fromFile(dd[1], filename: "$filename"),
});
var response = await dio.post(
'http://10.0.2.2/test/upload.php',
data: formData,
onSendProgress: (received, total) {
if (total != -1) {
print((received / total * 100).toStringAsFixed(0) + "%");
}
},
);
print(response);
Example 2: flutter get image file from assets
import 'dart:async';
import 'dart:io';
import 'package:flutter/services.dart' show rootBundle;
import 'package:path_provider/path_provider.dart';
Future<File> getImageFileFromAssets(String path) async {
final byteData = await rootBundle.load('assets/$path');
final file = File('${(await getTemporaryDirectory()).path}/$path');
await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
return file;
}
File f = await getImageFileFromAssets('images/myImage.jpg');