如何将文件从颤振发送到 django 应用程序?

时间:2021-02-23 19:30:45

标签: django flutter dart flutter-dependencies dio

我正在尝试将 pptx 文件发送到此 link 处的 django 服务器

这是原始 django 应用程序中的 html 表单:

<form method="post" enctype="multipart/form-data" action="find_file_type">
{% csrf_token %}
<div>
    <label class = "custom-file-upload" >
        Choose file
        <input name = "filePath" type="file" accept = ".pdf , application/vnd.ms-powerpoint , application/vnd.openxmlformats-officedocument.presentationml.presentation" id="file">
    </label>
    <input type="submit" value="Make it dark" id= "submit">
</div>

</form>

我无法将文件发送到此服务器,也不知道出了什么问题。 这是我使用 dio 包尝试过的:

void sendRequest() async{
    FormData formData = new FormData.fromMap({
      "file": await MultipartFile.fromFile(file.path,filename: filename)});
    response = await dio.post("https://nitemode.herokuapp.com/find_file_type", data: formData);
    print(response);
    if (response.statusCode == 200 || response.statusCode == 201) {
      print('Uploaded!');
    }
    print("Process complete");

  }
}

这是我得到的输出:

[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: DioError [DioErrorType.RESPONSE]: Http status error
 [403]
E/flutter (19891): #0      DioMixin._dispatchRequest (package:dio/src/dio.dart:966:7)
E/flutter (19891): <asynchronous suspension>
E/flutter (19891): #1      DioMixin._request._interceptorWrapper.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:di
o/src/dio.dart:849:37)
E/flutter (19891): #2      DioMixin.checkIfNeedEnqueue (package:dio/src/dio.dart:1121:22)
E/flutter (19891): #3      DioMixin._request._interceptorWrapper.<anonymous closure>.<anonymous closure> (package:dio/src/dio.dart:846:2
2)
E/flutter (19891): #4      new Future.<anonymous closure> (dart:async/future.dart:174:37)
E/flutter (19891): #5      _rootRun (dart:async/zone.dart:1182:47)
E/flutter (19891): #6      _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (19891): #7      _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (19891): #8      _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (19891): #9      _rootRun (dart:async/zone.dart:1190:13)
E/flutter (19891): #10     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (19891): #11     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
E/flutter (19891): #12     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter (19891): #13     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
E/flutter (19891): #14     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
E/flutter (19891): #15     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter (19891):

我不知道我做错了什么。 django 应用程序在文件上传后发送下载,这就是我收到此错误的原因吗?由于 django 应用程序不发送 200 或 203 响应,但响应如下:

response = HttpResponse(myfile, content_type='application/[vnd.openxmlformats-officedocument.presentationml.presentation]')
    response['Content-Disposition'] = 'attachment; filename='  + 'DarkFile.pptx'
    

请帮帮我,我不知道我做错了什么。 我也阅读了 dio 的文档,但不知道我做错了什么 我也试过 dio 中的 download() 函数,但没有下载文件。 我还一起完成了一个 post() 和一个 donwload() 函数,但也没有任何作用。

0 个答案:

没有答案