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
相关产品推荐
相关产品推荐

