如何在颤振中上传图像

时间:2021-05-24 05:53:11

标签: django flutter dart restapi

你好,我想在flutter中上传图片

我尝试使用 http.MultipartRequest 像这样

  request.fields["name"] = "$RegisterName";
  request.fields["description"] = "$RegisterDescription";
  request.fields["caution"] = "$RegisterCaution";
  request.fields["price"] = "$RegisterPrice";
  request.fields["price_prop"] = "$RegisterPriceProp";
  request.fields["user.id"] = "1";
  request.fields["lend"] = "$RegisterCategory";
  request.fields["category"] = "Digital";
  request.fields["place_option"] = "true";

  var multipartFile = http.MultipartFile.fromBytes(
    'file',
    (await rootBundle.load('assets/images/main_1.jpg')).buffer.asUint8List(),
    filename: 'test01.jpg',
    contentType: MediaType('image', 'jpg'),
  );

  request.files.add(multipartFile);
  

  var response = await request.send();

  if (response.statusCode == 200) print('Upload');
}

但是此代码不起作用 如果我使用此代码,只上传另一个数据 upload things

那么json类型就是这个 json type image

我想上传图片文件...:(

2 个答案:

答案 0 :(得分:0)

我用它来发送带有 formData 的图片

var head = Api().bearerHeader; ////just bearerToken
var request = http.MultipartRequest(
    'POST',
    Uri.parse(
        'https://c.....'));
request.files
    .add(await http.MultipartFile.fromPath('TITLEOFFORMDATA', imageFile.path));
request.headers.addAll(head);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  String varo = await response.stream.bytesToString();
}

答案 1 :(得分:0)

这是如何使用带有 MultipartRequest 包的 http 将图像发送到您的服务器

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.7.0/d3.min.js"></script>

<svg width="600" height="600">
  <defs>
    <marker id="arrowhead-to" markerWidth="10" markerHeight="7" 
    refX="10" refY="3.5" orient="auto">
      <polygon fill="blue" points="0 0, 10 3.5, 0 7" />
    </marker>
    <marker id="arrowhead-from" markerWidth="10" markerHeight="7" 
    refX="0" refY="3.5" orient="auto">
      <polygon fill="blue" points="10 0, 0 3.5, 10 7" />
    </marker>
  </defs>
</svg>