我具有上载图像的功能,但是服务器未收到任何信息,并且得到500状态代码。我确定服务器正常。当我从邮递员发送发帖请求时,它起作用! 这是我的功能:
uploadPrescriptionToAll(File file, data) async {
String convertedFilePath = await convertImage(file);
String token = await getToken();
Response response;
Dio dio = new Dio();
dio.options.baseUrl = "http://x.x.x.x:x";
FormData formData = new FormData.from({
"image":
new UploadFileInfo(new File(convertedFilePath), "image.jpg"),
"data": data,
});
try {
response = await dio.post("/api/images",
data: formData,
options: Options(headers: {
"Authorization": token,
"Content-Type": "multipart/form-data"
}));
} catch (e) {
print("Error Upload: " + e.toString());
}
print("Response Upload:" + response.toString());
}
我如何正确发布文件(表单数据)?还有另一种方法吗?
答案 0 :(得分:0)
尝试使用该文件:
var head = {"Authorization": "Bearer ${token} "};
var response = await dio.post(api,
data: formData, options: Options(headers: head));
FormData formData = new FormData.from({"file": new UploadFileInfo(image, image.path)});
答案 1 :(得分:0)
使用 Dio 使用以下方法非常简单: FormData.fromMap()
searchCityByName(String city) async {
Dio dio = new Dio();
var a = {"city": city};
var res = await dio.post(apiSearchState, data:FormData.fromMap(a));
}
答案 2 :(得分:0)
简而言之,您应该将 Map<String, dynamic>
对象传递给 dio.post()
的 data
字段。例如:
response = await dio.post("/api/images",
data: {"image": "image.jpg", "data": data});