Sending a list of images along with data in http post flutter.

huangapple go评论102阅读模式
英文:

Sending a list of images along with data in http post flutter

问题

我有一组图像和数据(字符串),需要发送到我的后端。这是重要的函数:

  1. Future MakePostPetOfferRequest(
  2. {required String title,
  3. required String description,
  4. required String price,
  5. required String city,
  6. required String species,
  7. required String sex,
  8. required String ageYears,
  9. required String ageMonths,
  10. required String breed,
  11. required List<File?> images}) async {
  12. Uri url = Uri.parse('http://$ip/create_sale_post');
  13. Map<String, dynamic> body = {
  14. "Title": title,
  15. "Description": description,
  16. "City": city,
  17. "Species": species,
  18. "Price": price,
  19. "Sex": sex,
  20. "Years": ageYears,
  21. "Months": ageMonths,
  22. "Breed": breed,
  23. "images": images,
  24. };
  25. var response = await http.post(url, headers: {"cookie": sessionCookie}, body: body);
  26. if (response.headers['set-cookie'] != null) {
  27. String initSessionCookie = response.headers['set-cookie']!;
  28. if (sessionCookie != "") sessionCookie = initSessionCookie.substring(0, initSessionCookie.indexOf(";"));
  29. }
  30. return response;
  31. }

但是我遇到了不同的错误,首先是错误:

类型 'List<File?>' 不是 'String' 类型的子类型

我也不确定如何将图像添加到一个单一字段,就像在 Postman 中这样做:
Sending a list of images along with data in http post flutter.

希望问题清楚,感谢您的帮助。

英文:

I have a list of images and data (string) that I need to send to my backend.
This is the important function:

  1. Future MakePostPetOfferRequest(
  2. {required String title,
  3. required String description,
  4. required String price,
  5. required String city,
  6. required String species,
  7. required String sex,
  8. required String ageYears,
  9. required String ageMonths,
  10. required String breed,
  11. required List&lt;File?&gt; images}) async {
  12. Uri url = Uri.parse(&#39;http://$ip/create_sale_post&#39;);
  13. Map&lt;String, dynamic&gt; body = {
  14. &quot;Title&quot;: title,
  15. &quot;Description&quot;: description,
  16. &quot;City&quot;: city,
  17. &quot;Species&quot;: species,
  18. &quot;Price&quot;: price,
  19. &quot;Sex&quot;: sex,
  20. &quot;Years&quot;: ageYears,
  21. &quot;Months&quot;: ageMonths,
  22. &quot;Breed&quot;: breed,
  23. &quot;images&quot;: images,
  24. };
  25. var response = await http.post(url, headers: {&quot;cookie&quot;: sessionCookie}, body: body);
  26. if (response.headers[&#39;set-cookie&#39;] != null) {
  27. String initSessionCookie = response.headers[&#39;set-cookie&#39;]!;
  28. if (sessionCookie != &quot;&quot;) sessionCookie = initSessionCookie.substring(0, initSessionCookie.indexOf(&quot;;&quot;));
  29. }
  30. return response;
  31. }

But I am faced with different errors, the first is the error:
>type 'List<File?>' is not a subtype of type 'String' in type cast

I also am not sure how I can add the images to one single field, like I do it on Post man here:
Sending a list of images along with data in http post flutter.

I hope the question is clear and thank you for the assistance.

答案1

得分: 2

要上传图片,你应该使用表单数据(formdata)。

  1. import 'package:dio/dio.dart' as formDataMap;
  2. formDataMap.FormData formData = formDataMap.fromMap({
  3. "Title": title,
  4. "Description": description,
  5. "City": city,
  6. "Species": species,
  7. "Price": price,
  8. "Sex": sex,
  9. "Years": ageYears,
  10. "Months": ageMonths,
  11. "Breed": breed
  12. });
  13. for (var file in images) {
  14. formData.files.addAll([
  15. MapEntry("images[]", await formDataMap.MultipartFile.fromFile(file.path)),
  16. ]);
  17. }

这段代码用于创建表单数据,以便上传图像。

英文:

To post image you should use a formdata.

  1. import &#39;package:dio/dio.dart&#39; as formDataMap;
  2. formDataMap.FormData formData = formDataMap.fromMap({
  3. &quot;Title&quot;: title,
  4. &quot;Description&quot;: description,
  5. &quot;City&quot;: city,
  6. &quot;Species&quot;: species,
  7. &quot;Price&quot;: price,
  8. &quot;Sex&quot;: sex,
  9. &quot;Years&quot;: ageYears,
  10. &quot;Months&quot;: ageMonths,
  11. &quot;Breed&quot;: breed
  12. });
  13. for (var file in images) {
  14. formData.files.addAll([
  15. MapEntry(&quot;images[]&quot;,
  16. await formDataMap.MultipartFile.fromFile(file.path)),
  17. ]);
  18. }

答案2

得分: 0

  1. 我已找到一个方法来实现这个,而且它非常简单,只使用了内置的 *http.MultipartRequest()* 方法。
  1. Uri url = Uri.parse('http://$ip/create_sale_post');
  2. var request = http.MultipartRequest('POST', url);
  3. for (var i = 0; i &lt; images.length; i++) {
  4. var image = await http.MultipartFile.fromPath(
  5. 'images',
  6. images[i]!.path,
  7. );
  8. request.files.add(image);
  9. }
  10. request.headers['cookie'] = sessionCookie;
  11. request.fields["Title"] = title;
  12. request.fields["Description"] = description;
  13. request.fields["City"] = city;
  14. request.fields["Species"] = species;
  15. request.fields["Price"] = price;
  16. request.fields["Sex"] = sex;
  17. request.fields["Years"] = ageYears;
  18. request.fields["Months"] = ageMonths;
  19. request.fields["Breed"] = breed;
  20. request.headers['Content-Type'] = 'multipart/form-data';
  21. var response = await request.send();
  1. <details>
  2. <summary>英文:</summary>
  3. I have found a way to do it, and it is fairly simple, using no third party packages, only the built in *http.MultipartRequest()* method.
  1. Uri url = Uri.parse(&#39;http://$ip/create_sale_post&#39;);
  2. var request = http.MultipartRequest(&#39;POST&#39;, url);
  3. for (var i = 0; i &lt; images.length; i++) {
  4. var image = await http.MultipartFile.fromPath(
  5. &#39;images&#39;,
  6. images[i]!.path,
  7. );
  8. request.files.add(image);
  9. }
  10. request.headers[&#39;cookie&#39;] = sessionCookie;
  11. request.fields[&quot;Title&quot;] = title;
  12. request.fields[&quot;Description&quot;] = description;
  13. request.fields[&quot;City&quot;] = city;
  14. request.fields[&quot;Species&quot;] = species;
  15. request.fields[&quot;Price&quot;] = price;
  16. request.fields[&quot;Sex&quot;] = sex;
  17. request.fields[&quot;Years&quot;] = ageYears;
  18. request.fields[&quot;Months&quot;] = ageMonths;
  19. request.fields[&quot;Breed&quot;] = breed;
  20. request.headers[&#39;Content-Type&#39;] = &#39;multipart/form-data&#39;;
  21. var response = await request.send();
  1. </details>

huangapple
  • 本文由 发表于 2023年4月4日 08:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定