【发布时间】:2019-11-22 03:43:32
【问题描述】:
我正在尝试使用 Flutter 的 http 插件上传图片。
以下是我的代码
Future<ImageUploadModel> postImage(File photoPath) async {
ImageUploadModel imageUploadModel;
var request = http.MultipartRequest(
"POST", Uri.parse("my_api_url"));
request.files.add(http.MultipartFile.fromBytes(
"photo", await File.fromUri(Uri.parse(photoPath.path)).readAsBytes(),
contentType: MediaType('image', 'jpg')));
final response = await request.send();
Uint8List responseByteArray = await response.stream.toBytes();
return standardSerializers.deserializeWith(ImageUploadModel.serializer, json.decode(utf8.decode(responseByteArray)));
}
我尝试使用 Postman 上传图片,并且图片已正确上传。
我已关注堆栈上的链接,但我无法解决错误
Flutter: http post upload an image Flutter how to send multiple files to http post How to send an image to an api in dart/flutter? How to upload image in Flutter?
我得到一个 200 的状态码,但是当我试图从响应中访问任何变量时,我得到一个空字符串。
我尝试过使用 Kotlin 和 Retrofit 的 Native Android,它工作正常
我也尝试了Dio 库
Dio dio = Dio();
FormData formdata = FormData();
formdata.add("photo", UploadFileInfo(photoPath, basename(photoPath.path)));
dio
.post("My_API_URL",
data: formdata,
options: Options(
method: 'POST',
responseType: ResponseType.json // or ResponseType.JSON
))
.then((response) {
print("2 $response");
return standardSerializers.deserializeWith(
ImageUploadModel.serializer, json.decode(response.data));
}).catchError((error) => print("3 $error"));
我不想将我的图像转换为 base64
【问题讨论】: