本文介绍了如何在Flutter中将图像和文件上传到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Web服务进行图像处理,在邮递员中效果很好:
I use a web service for image processing , it works well in Postman:
现在我想用Dart发出http请求:
Now I want to make http request in flutter with Dart:
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;
}
但我不知道如何上传图像文件,在上面的代码中我得到了例外:错误的状态:无法设置内容类型为 multipart / form-data的请求的正文字段。
我应该如何编写请求正文?
but I don't know how to upload the image file, in above code I get exception: Bad state: Cannot set the body fields of a Request with content-type "multipart/form-data".
How should I write the body of request?
推荐答案
我想推荐包给您,非常易于使用,在这种情况下,您可以:
dio is very easy to use, in this case you can:
发送FormData:
FormData formData = new FormData.from({
"name": "wendux",
"file1": new UploadFileInfo(new File("./upload.jpg"), "upload1.jpg")
});
response = await dio.post("/info", data: formData)
更多详细信息,请参阅。
More details please refer to dio。
这篇关于如何在Flutter中将图像和文件上传到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!