Reputation: 219
I am having array of image to upload to sever when i upload a set of images its uploading last image only how can i solve this,
void uploads() async
{
var multipartFile;
var uri = Uri.parse( Config.URL + "Posts/Cube_Post_Submit");
var request = new http.MultipartRequest("POST", uri);
for(int i=0;i<image.length;i++)
{
print(image[i]);
var stream = new
http.ByteStream(DelegatingStream.typed(image[i].openRead()));
var length = await image[i].length();
multipartFile = new http.MultipartFile('attachments', stream, length,
filename: basename(image[i].path));
} request.files.add(multipartFile);
request.fields['User_Id'] = Config.USER_ID;
request.fields['Cubes_Id'] = jsonstring;
request.fields['Post_Text'] = "hello";
request.fields['Post_Category'] = "Story";
request.fields['Post_Link'] = "";
var response = await request.send();
print(response.statusCode);
response.stream.transform(utf8.decoder).listen((value)
{
print(value);
});
}
Upvotes: 1
Views: 3640
Reputation: 51760
You are adding the multipartFile
to request.files
outside the for loop. Move it inside...
void uploads() async {
var uri = Uri.parse(Config.URL + 'Posts/Cube_Post_Submit');
var request = http.MultipartRequest('POST', uri);
for (int i = 0; i < image.length; i++) {
request.files.add(
http.MultipartFile(
'attachments$i',
http.ByteStream(DelegatingStream.typed(image[i].openRead())),
await image[i].length(),
filename: basename(image[i].path),
),
);
}
request.fields['User_Id'] = Config.USER_ID;
request.fields['Cubes_Id'] = jsonstring;
request.fields['Post_Text'] = 'hello';
request.fields['Post_Category'] = 'Story';
request.fields['Post_Link'] = '';
var response = await request.send();
print(response.statusCode);
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
}
Upvotes: 5