Dart上传文件到asp.net核心

rnmwe5a2  于 2023-07-31  发布在  .NET
关注(0)|答案(1)|浏览(89)

我有一个dart函数,看起来像这样:

Future<http.StreamedResponse> httpFilePost(Uri uri,
      {String fileName, String key, Uint8List fileBytes}) async {
    var delay = new Duration(milliseconds: 200);
    var tokenRetried = false;
    while (true) {
      final token = await getRefreshToken();

      final request = http.MultipartRequest("POST", uri);
      request.files.add(new http.MultipartFile.fromBytes(key, fileBytes));
      if (token != null && token.isNotEmpty)
        request.headers['authorization'] = 'bearer $token';

      final response = await retry(() => request.send(),
          retryIf: (e) => e is SocketException || e is TimeoutException);

      if (response.statusCode == 503 || response.statusCode == 502) {
        await Future.delayed(delay);
        delay = new Duration(milliseconds: delay.inMilliseconds * 2);
      } else if (response.statusCode == 401) {
        if (!tokenRetried && refreshToken != null && await refreshToken()) {
          tokenRetried = true;
          continue;
        }

        throw HttpSecurityException(
            "The user is not authorized to access the endpoint",
            uri: uri);
      } else {
        return response;
      }
    }
  }

字符串
基本上,它附加刷新头,然后上传文件。key是web API方法中的参数名称,如下所示:

[HttpPost("UploadVideo"), DisableRequestSizeLimit]
public async Task<IActionResult> UploadVideo([FromQuery] string contentId, IFormFile file, [FromServices] IOptions<ConnectionStrings> connectionStrings, [FromQuery] Languages language = Languages.English, CancellationToken cancellationToken = default)


我想,这很容易,我会完成的。
除了asp.net核心抛出的文件字段是必需的。
我检查了原始请求,它似乎是正确的,并清楚地标记了文件字段。
我做错了什么?

zpgglvta

zpgglvta1#

您需要将文件名添加到:
第一个月
变更为:
request.files.add(new http.MultipartFile.fromBytes(key, fileBytes, filename: filename));

相关问题