相关疑难解决方法(0)

Flutter:http post上传图片

我使用Web服务进行图像处理,它在Postman中运行良好:

邮差截图

现在我想用Dart扑灭http请求:

import 'package:http/http.dart' as http;

static ocr(File image) async {
    var url = '${API_URL}ocr';
    var bytes = image.readAsBytesSync();

    var response = await http.post(
        url,
        headers:{ "Content-Type":"multipart/form-data" } ,
        body: { "lang":"fas" , "image":bytes},
        encoding: Encoding.getByName("utf-8")
    );

    return response.body;

  }
Run Code Online (Sandbox Code Playgroud)

但我不知道如何上传图像文件,在上面的代码中我得到异常:Bad state: Cannot set the body fields of a Request with content-type "multipart/form-data".
我应该如何编写请求体?

image-uploading httprequest dart flutter

20
推荐指数
9
解决办法
2万
查看次数

标签 统计

dart ×1

flutter ×1

httprequest ×1

image-uploading ×1