Reputation: 169
I have a List of images as bytes and I would like to send it as a post request in flutter web.
Firstly how do I send the data in file format just like image picker for android/iOS in flutter web
Secondly if I had to send the attachments in the below code what changes would I need to make in multipart in flutter web
If anyone can please help me resolve this.
Let me know if you require any further information from my end
dataInsert.dart
File _file = File("zz");
Uint8List webImage = Uint8List(10);
else if (kIsWeb) {
final imagefile =
await picker.pickImage(source: ImageSource.gallery, maxWidth: 600);
if (imagefile != null) {
var f = await imagefile.readAsBytes();
setState(() {
_file = File("a");
webImage = f;
_storedImageWeb.add(webImage);
});
}
}
Below is the value of webImage on running the above code :
[255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 255, 226, 2, 40, 73, 67, 67, 95, 80, 82, 79, 70, 73, 76, 69, 0, 1, 1, 0, 0, 2, 24, 0, 0, 0, 0, 4, 48, 0, 0, 109, 110, 116, 114, 82, 71, 66, 32, 88, 89, 90, 32, 0, 0, 0, 0, 0, 0, 0, 0]
Multipart.dart
Future<void> addUser(
ProjectDetail stnotification, List<Uint8List> imagecheckb) async {
Map<String, String> headers = {
"Content-Type": "charset=utf-8",
"Content-type": "application/json"
};
var uri = Uri.parse('http://localhost:4000/check/upload');
try {
var request = http.MultipartRequest('POST', uri);
request.headers.addAll(headers);
for (int i = 0; i <= imagecheckb.length - 1; i++) {
http.MultipartFile multipartFile = await http.MultipartFile.fromBytes(
'attachments', imagecheckb[i].cast());
request.files.add(multipartFile);
}
Upvotes: 0
Views: 1225
Reputation: 415
You can handle the list of image by index:
Future addProduct(String name, String description, List<File> images) async{
final request = await http.MultipartRequest(
'POST',
Uri.parse('$baseUrl/products/add-product'),
);
request.headers.addAll(header);
request.fields['Name'] = name;
request.fields['Description'] = description;
for(int i = 0; i < images.length; i++){
final f = await http.MultipartFile.fromPath('Photos[$i]',images[i].path);
request.files.add(f);
}
final responseStream = await request.send();
final response = await http.Response.fromStream(responseStream);
if(response.statusCode == 200) {
print("success")
} else {
print('failed')
}
});
Upvotes: 0
Reputation: 34
This is a way to send a list of files/images from multipart.
await Future.forEach(
files,
(file) async => {
request.files.add(
http.MultipartFile(
'files',
(http.ByteStream(file.openRead())).cast(),
await file.length(),
filename: basename(file.path),
),
)
},
);
Upvotes: 0