当我上传一组图像时,我要上传的图像数组要发送给服务器,它只能上传最后一张图像,
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);
});
}
答案 0 :(得分:3)
您正在将multipartFile
添加到request.files
的for循环之外。将其移入...
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',
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);
});
}