【问题标题】:Error Uploading file to server with Flutter使用 Flutter 将文件上传到服务器时出错
【发布时间】:2021-06-02 10:52:56
【问题描述】:

我有一个颤振应用程序,它使用 asp.net rest api 选择音频文件并将其上传到服务器。

我的flutter代码如下

 uploadFile() async {
    print(file.path);
    var postUri = Uri.parse("http://192.168.1.100:5041/api/fileup/up");
    var request = new http.MultipartRequest("POST", postUri);
    request.fields['user'] = 'blah';
    request.files.add(new http.MultipartFile.fromBytes('file', await File.fromUri(Uri.parse(file.path)).readAsBytes(),contentType: MediaType('audio','mp3')
    ));

    request.send().then((response) {
      if (response.statusCode == 200) {
        print("Uploaded!");
      }else{
        print(response.reasonPhrase);
      }
    });
  }

上述颤振代码的我的 file.path 值为“/data/user/0/com.mydomain.myappname/cache/file_picker/Over_the_Horizo​​n.mp3”,它是从文件选择器返回的。

我可以使用邮递员上传文件,但颤动代码给我 500:内部服务器错误

邮递员截图

尝试了几个在堆栈溢出时发现的代码,都给了我同样的错误

【问题讨论】:

  • 你好,我的情况和你一样,你用dio包后效果如何,还有问题吗?

标签: flutter asp.net-web-api file-upload


【解决方案1】:

你尝试过 DIO 吗?

您可以使用 DIO 完成此操作,如下所示:

FormData formData = new FormData.fromMap({
    "file": await MultipartFile.fromFile(file.path,
        filename: file.path.split('/').last)
  });
await dio.post('http://192.168.1.100:5041/api/fileup/up', data: formData, onSendProgress: (int begin, int end) {
  var initial = begin;
  var done = end;
  print('$initial $end');
});

【讨论】:

  • 我认为我的 file.path 值没有正确传递
猜你喜欢
  • 1970-01-01
  • 2014-10-10
  • 2013-10-05
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-08
相关资源
最近更新 更多