dioerror(dioerror [dioerrortype.other]:不良状态:可以确定最终确定的多片file

发布于 2025-01-20 06:43:47 字数 1251 浏览 0 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2025-01-27 06:43:47

避免重复使用最终确定的多部分对象
如果您要为多个请求重复使用相同的多file对象,请确保为每个请求创建一个新实例。试图最终确定已经完成的多file可能会导致您面临的错误。

MultipartFile file = await MultipartFile.fromFile('path/to/file');

// Incorrect: Reusing the same MultipartFile object across requests
FormData formData1 = FormData.fromMap({
  'file1': file,
});

FormData formData2 = FormData.fromMap({
  'file2': file,
});

// Correct: Create a new MultipartFile instance for each request
MultipartFile file1 = await MultipartFile.fromFile('path/to/file1');
MultipartFile file2 = await MultipartFile.fromFile('path/to/file2');

FormData formData1 = FormData.fromMap({
  'file1': file1,
});

FormData formData2 = FormData.fromMap({
  'file2': file2,
});

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.

MultipartFile file = await MultipartFile.fromFile('path/to/file');

// Incorrect: Reusing the same MultipartFile object across requests
FormData formData1 = FormData.fromMap({
  'file1': file,
});

FormData formData2 = FormData.fromMap({
  'file2': file,
});

// Correct: Create a new MultipartFile instance for each request
MultipartFile file1 = await MultipartFile.fromFile('path/to/file1');
MultipartFile file2 = await MultipartFile.fromFile('path/to/file2');

FormData formData1 = FormData.fromMap({
  'file1': file1,
});

FormData formData2 = FormData.fromMap({
  'file2': file2,
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文