【发布时间】:2019-12-28 09:51:49
【问题描述】:
我正在尝试通过 post 方法通过颤振上传图像。我正在使用 image_picker 从手机中挑选文件,但我无法上传
我已经尝试发送 FormData 之类的文件也不起作用
Future<dynamic> uploadLicence(int id ,dynamic obj) async {
FormData formdata = new FormData(); // just like JS
formdata.add("image",obj);
final response = await post('Logistic/driver/LicenceImage?
driverId=$id',
formdata);
print(response);
// return null;
if (response.statusCode == 200) {
final result = json.decode(response.body);
return result;
} else {
return null;
}
}
在那之后,我只是尝试了这种方法,但这也不起作用
Future<dynamic> uploadLicence(int id, File file) async {
final url = Uri.parse('$BASE_URL/Logistic/driver/LicenceImage?
driverId=$id');
final fileName = path.basename(file.path);
final bytes = await compute(compress, file.readAsBytesSync());
var request = http.MultipartRequest('POST', url)
..files.add(new http.MultipartFile.fromBytes(
'image',bytes,filename: fileName,);
var response = await request.send();
var decoded = await
response.stream.bytesToString().then(json.decode);
if (response.statusCode == HttpStatus.OK) {
print("image uploded $decoded");
} else {
print("image uplod failed ");
}
}
List<int> compress(List<int> bytes) {
var image = img.decodeImage(bytes);
var resize = img.copyResize(image);
return img.encodePng(resize, level: 1);
}
【问题讨论】:
-
你得到什么错误?你打印了
decoded变量吗? -
@ Esen Mehmet before
decoded我收到此错误未处理异常:无效参数:隔离消息中的非法参数:(对象是一个闭包 - 函数“压缩”:.) -
改变这个:compute(compress, file.readAsBytesSync()) => compute(compress(), file.readAsBytesSync())。您正在使用 compress 方法作为参考,而不是使用它的返回值。
-
当我通过
compress()时显示错误compress(List<int> bytes) → List<int> The argument type 'List<int>' can't be assigned to the parameter type '(List<int>) → FutureOr<dynamic> -
我认为问题在于
bytes变量。你可以尝试用这个函数替换bytes变量吗?compress(await file.readAsBytesSync())我必须模拟这个才能给出精确的解决方案。