Flutter DIO:使用带Dio包的二进制文件上传图像 [英] Flutter DIO: upload image using binary body with Dio package
问题描述
通过http包,我可以通过将二进制数据放在post调用的正文中(例如此代码的片段中)来将图像发送到服务器:
With thwe http package I can send an image to a server by putting te binary data in the body of a post call like in the snippet of this code:
var response = await http.post('My_url', body: File(path).readAsBytesSync(), headers: {
'apikey': 'myAPIKEY',
'Content-Type': 'image/*', // set content-length
});
我不能通过使用Dio来做同样的事情,我不知道如何将二进制数据直接放入体内(就像我可以用邮递员一样)
I can't do the same thing by using Dio, I don't know how to put directly the binary data in the body (like i can do it with postman)
推荐答案
如果有人偶然发现同一问题,只需提出我的解决方案.
Just putting my solution if someone stumbles upon the same issue.
我必须在已签名的Google存储URL上上传文件.将文件二进制数据插入PUT请求主体所需的API.无法使用DIO插件实现,我使用DART HTTP包解决了该问题,下面是示例代码.
I had to upload the file at a signed google storage URL. API required to insert the file binary data in the body of the PUT request. Couldn't implement using the DIO plugin, I resolved the issue using the DART HTTP package, Below is a sample code.
import 'package:http/http.dart' as http;
await http.put(
Uri.parse(uploadURL),
headers: {
'Content-Type': mimeType,
'Accept': "*/*",
'Content-Length': File(filePath).lengthSync().toString(),
'Connection': 'keep-alive',
},
body: File(filePath).readAsBytesSync(),
);
这篇关于Flutter DIO:使用带Dio包的二进制文件上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!