gpt4 book ai didi

flutter - 将图像数组上传到服务器

转载 作者:IT王子 更新时间:2023-10-29 07:08:39 25 4
gpt4 key购买 nike

当我上传一组图像时,我有一组图像要上传到服务器,它只上传最后一张图像我该如何解决这个问题,

  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);
});
}

最佳答案

您正在将 multipartFile 添加到 for 循环外的 request.files。把它移到里面...

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);
});
}

关于flutter - 将图像数组上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52534345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com