如何解决在颤动中将图像上传到服务器时出错
我正在通过从画廊或相机中挑选图片将图像上传到服务器。但是我从服务器端获取文件未找到错误意味着文件路径变为空白或存在另一个问题。我也在使用 image_cropper 裁剪图像,我还发送同样位于下方的标题 - 图像已成功上传到邮递员表单数据
void _showPickOptionsDialog(BuildContext context) {
showDialog(
context: context,builder: (context) => AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,children: <Widget>[
ListTile(
title: Text("Pick from Gallery"),onTap: () {
_loadPicker(ImageSource.gallery);
},),ListTile(
title: Text("Take a picture"),onTap: () {
_loadPicker(ImageSource.camera);
},)
],);
}
_loadPicker(ImageSource source) async {
_pickedImage = await ImagePicker.pickImage(source: source);
if (_pickedImage != null) {
_cropImage(File(_pickedImage.path));
}
Navigator.pop(context);
}
_cropImage(File picked) async {
File cropped = await ImageCropper.cropImage(
androidUiSettings: AndroidUiSettings(
statusBarColor: Colors.red,toolbarColor: Colors.red,toolbarTitle: "Crop Image",toolbarWidgetColor: Colors.white,sourcePath: picked.path,aspectRatioPresets: [
CropAspectRatioPreset.original,CropAspectRatioPreset.ratio16x9,CropAspectRatioPreset.ratio4x3,CropAspectRatioPreset.ratio3x2,],maxWidth: 512,maxHeight: 512,);
if (cropped != null) {
setState(() {
_pickedImage = cropped;
print("profilePath" + _pickedImage.toString());
uploadFile(_pickedImage.path);
// uploadImage(_pickedImage.path);
});
}
}
api 调用
Future<ProfileUpdateModel> uploadFile(file) async {
var postUri = Uri.parse(urlProfileUpdate);
var request = new http.MultipartRequest("POST",postUri);
ProgressDialog pr = ProgressDialog(context);
pr.style(
message: 'Uploading',borderRadius: 10.0,backgroundColor: Colors.white,progressWidget: CircularProgressIndicator(),elevation: 10.0,progressTextStyle: TextStyle(
color: Colors.black,fontSize: 13.0,fontWeight: FontWeight.w400),messageTextStyle: TextStyle(
color: Colors.black,fontSize: 18.0,fontWeight: FontWeight.w600));
await pr.show();
request.fields['user_id'] = '1';
// request.files.add(new http.MultipartFile.fromBytes('file',await File.fromRawPath(file).readAsBytes(),// contentType: new MediaType('image',".jpg")));
request.files.add(http.MultipartFile('file',File(file).readAsBytes().asStream(),File(file).lengthSync(),filename: file.split("/").last));
request.headers.addAll(BaseURLHeaders().getHeader2());
var streamedResponse = await request.send();
var response = await http.Response.fromStream(streamedResponse);
print("responcebodyprofile " + response.body);
if(response.statusCode == 200){
pr.hide();
var res = json.decode(response.body);
ProfileUpdateModel _userProfileUpdateModel = ProfileUpdateModel.fromJson(res);
if(_userProfileUpdateModel.success == 1){
CustomClass().makeToast(_userProfileUpdateModel.message,context);
getData();
}
}else{
pr.hide();
}
}
标题
Map getHeader2() {
final Map<String,String> headers = {
"Content-type": "multipart/form-data","api_key": "demo","secret_key": "demo",};
return headers;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。