You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中使用MultipartFile.fromBytes发起PUT请求失败排查

Flutter HTTP上传PDF文件问题解决方法
  • 显式构建MultipartFile字节流
    跳过MultipartFile.fromBytes的封装,手动传入字节流、文件长度和内容类型,确保二进制流被正确初始化:

    var request = http.MultipartRequest('PUT', Uri.parse('https://xxx/files'))
      ..fields['grade'] = 'xxx'
      ..fields['candidate'] = 'xxx';
    
    final byteStream = http.ByteStream.fromBytes(file.bytes!);
    final multipartFile = http.MultipartFile(
      'document',
      byteStream,
      file.bytes!.length,
      contentType: MediaType('application', 'pdf'),
    );
    request.files.add(multipartFile);
    
    var response = await request.send();
    
  • 确认API支持PUT方法上传
    部分后端仅接受POST方法处理文件上传,若修改后仍失败,尝试将请求方法改为POST:

    var request = http.MultipartRequest('POST', Uri.parse('https://xxx/files'))
    // 其余字段和文件添加逻辑不变
    
  • 核对请求字段名与API定义一致
    确保grade、candidate、document这些参数名和后端API要求完全匹配,拼写错误会导致数据无法被正确识别。

内容的提问来源于stack exchange,提问作者John Rick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 02:20:48