【问题标题】:Choose a file (image, pdf, doc) and upload to server flutter android选择一个文件(图片、pdf、doc)并上传到服务器flutter android
【发布时间】:2021-07-28 08:58:57
【问题描述】:

我正在使用 ASP.NET REST API。任务是我只需要选择一件事,即图像、pdf、docs 文件并将其发送到服务器。对于挑选文件,我使用以下库 file_picker:^3.0.3

当我将文件发送到服务器时成功挑选文件后,服​​务器的响应是403禁止。

// this is picking image code
ElevatedButton(
            onPressed: () async {
              FilePickerResult result = await FilePicker.platform.pickFiles();

              if (result != null) {
                PlatformFile file = result.files.first;

                ApiClient.apiClient.uploadDocumentApi(file.path);
              }
            },
            style: ElevatedButton.styleFrom(
              primary: kPrimaryColor,
              elevation: 0.0,
            ),
            child: Text('Select'),
          ),

// this is API code
Future<void> uploadDocumentApi(String filePath) async {
    print('pathh: ' + filePath);
    String url = 'www.example.com';
    var request = http.MultipartRequest(
      'POST',
      Uri.parse(url),
    );
    // request.files.add(await http.MultipartFile.fromPath('', filePath));
    request.files.add(
      http.MultipartFile(
        '',
        File(filePath).readAsBytes().asStream(),
        File(filePath).lengthSync(),
        filename: filePath.split("/").last,
      ),
    );
    http.StreamedResponse response = await request.send();
    print(response.statusCode);
    print(response.reasonPhrase);
    if (response.statusCode == 200) {
      print('success');
      print(response.stream.bytesToString());
    } else {
      print('fail');
      print(response.reasonPhrase);
    }
  }

【问题讨论】:

    标签: flutter


    【解决方案1】:

    因为错误代码表明它与您的请求的身份验证有关。如果需要,请确保您在请求标头中正确设置您的 jwt,并在后端检查它

    【讨论】:

      猜你喜欢
      • 2014-09-03
      • 1970-01-01
      • 2021-06-15
      • 2012-03-10
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多