JS1*_*JS1 9 http multipartform-data dart flutter dio
我想将图片作为多部分文件发送到服务器。
首先我尝试使用http.post:
var response = await http.post(
Uri.parse('url.php'),
headers:{ "Content-Type":"multipart/form-data" } ,
body: {
"fichier": base64img
}
);
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
错误状态:无法设置内容类型为“multipart/form-data”的请求的正文字段。
查看本主题的答案,我尝试使用dio 包:
var dio = Dio();
var formData = FormData.fromMap({
'fichier': await MultipartFile.fromFile(filePath, filename: 'upload')
});
var response = await dio.post(
'url.php',
data: formData
);
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
错误:不支持的操作:仅在 dart:io 可用的情况下才支持 MultipartFile。
这里已经开放了一个问题。
最后,我尝试使用http 包中的MultipartRequest:
var url = Uri.parse('url.php');
var request = new http.MultipartRequest("POST", url);
request.files.add(
await http.MultipartFile.fromPath(
"fichier",
filePath
)
);
request.send().then((response) => print(response));
Run Code Online (Sandbox Code Playgroud)
得到与 dio 包完全相同的错误。
如果有人作为解决方案,我很乐意接受。
Rub*_*elo 17
使用 http 包并使用fromBytesintead获取图像fromPath:
final uri = Uri.parse('https://myendpoint.com');
var request = new http.MultipartRequest('POST', uri);
final httpImage = http.MultipartFile.fromBytes('files.myimage', bytes,
contentType: MediaType.parse(mimeType), filename: 'myImage.png');
request.files.add(httpImage);
final response = await request.send();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37711 次 |
| 最近记录: |