dioerror(dioerror [dioerrortype.other]:不良状态:可以确定最终确定的多片file
我正在尝试使用dio上传多个文件,在发送请求时,我收到错误:
DioError (DioError [DioErrorType.other]: Bad state: Can't finalize a finalized MultipartFile.
我的请求如下:
Future<String> sendRequest() async {
_className = classController.text;
_studentName = studentController.text;
_assnNum = assignmentController.text;
if (_className != null && _studentName != null && _assnNum != null) {
var url =
"http://157.245.141.117:8000/uploadfile?collection=$_className&assn_num=$_assnNum&student_name=$_studentName";
var uri = Uri.parse(url);
var formData = FormData();
for (var file in _files) {
print('FilePath: ${file.path}');
formData.files.addAll([
MapEntry("assignment", await MultipartFile.fromFile(file.path)),
]);
var response = await dio.post(
url,
data: formData,
options: Options(headers: {
HttpHeaders.contentTypeHeader: "application/x-www-form-urlencoded",
}),
);
print(response.statusCode);
}
}
return '';
}
我在200的API上收到一个状态,所有参数正在传递,但是这些文件没有在上传。我不确定从哪里开始。我正在上传CPP文件和Python文件,我发现的大多数示例都专门处理图像。我不确定如何进行。
I am attempting to upload multiple files using Dio, upon the request being sent I am receiving the error:
DioError (DioError [DioErrorType.other]: Bad state: Can't finalize a finalized MultipartFile.
My request is as follows:
Future<String> sendRequest() async {
_className = classController.text;
_studentName = studentController.text;
_assnNum = assignmentController.text;
if (_className != null && _studentName != null && _assnNum != null) {
var url =
"http://157.245.141.117:8000/uploadfile?collection=$_className&assn_num=$_assnNum&student_name=$_studentName";
var uri = Uri.parse(url);
var formData = FormData();
for (var file in _files) {
print('FilePath: ${file.path}');
formData.files.addAll([
MapEntry("assignment", await MultipartFile.fromFile(file.path)),
]);
var response = await dio.post(
url,
data: formData,
options: Options(headers: {
HttpHeaders.contentTypeHeader: "application/x-www-form-urlencoded",
}),
);
print(response.statusCode);
}
}
return '';
}
I am receiving a status on my api of 200, all params are being passed, but the files are not being uploaded. I'm not sure where to begin. I am uploading cpp files and python files, most examples I have found are dealing exclusively with images. I am unsure how to proceed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
避免重复使用最终确定的多部分对象
如果您要为多个请求重复使用相同的多file对象,请确保为每个请求创建一个新实例。试图最终确定已经完成的多file可能会导致您面临的错误。
Avoid reusing finalized MultipartFile objects
If you're reusing the same MultipartFile object for multiple requests, make sure to create a new instance for each request. Attempting to finalize a MultipartFile that has already been finalized can result in the error you're facing.