Flutter:我正在使用postapi上传一个文件到数据库.但它给了我一个bug



我正在使用文件选择器来选择文件。文件可以是png-jpg或pdf。

FilePickerResult? result =
await FilePicker.platform.pickFiles(
type: FileType.custom,
allowMultiple: true,
allowedExtensions: ['jpg', 'png', 'pdf'],
allowCompression: true,
);
setState(() {
if (result!.files.length != 0) {
_con!.thumbnail = result.files;
}
});

下面是使用Dio库使用post-api将其上传到数据库的代码

Future uploadFile(BuildContext context) async {
setState(() {
isFormLoading = true;
});
var R;
Dio D = Dio();
D.options.headers = {
'Content-type': 'multipart/form-data',
// 'Content-type': 'application/json',
'Accept': 'application/json',
};
FormData formData = FormData.fromMap({
"image": DD.MultipartFile.fromFile(thumbnail.elementAt(0).path!),
});
R = await D.post(BASE_URL + "api/v2/upload/21", data: formData);
dynamic Response = R.data;
print('Response is >>>>>>');
print(R);
// BotToast.showText(text: Response["success"].toString());
setState(() {
isFormLoading = false;
});

}

修改上传文件的扩展名(jpg,png,pdf(

var formData=formData.fromMap({

"file":
await MultipartFile.fromFile(path, filename: 'upload.jpg'),

});
Response response = await dio.post(
Constant().urlBase + Constant().confirm_package_reception,
data: formData);`enter code here`

相关内容

  • 没有找到相关文章