【发布时间】:2020-05-05 15:10:40
【问题描述】:
我正在使用这个 api 上传一个 mp3 文件
使用这个方法
Future<void> uploadRecord(String matchId, String filePath) async {
Uri url = Uri.parse(
Urls.baseurl + EndPoints.uploadRecordEndPoint + '${auth.token}');
final request = http.MultipartRequest('POST', url)
..fields['match_id'] = matchId
..files.add(http.MultipartFile.fromBytes(
'file', await File.fromUri(Uri(path: filePath)).readAsBytes(),
contentType: MediaType('audio', 'mpeg')));
final response = await request.send();
final responseStr = await response.stream.bytesToString();
print(responseStr);
}
但它不起作用,似乎没有文件上传,我错过了什么吗?还是有更好的解决方案?
【问题讨论】:
-
这不会解决您的问题,但您应该使用 MultipartFile 的另一个命名构造函数,它接受文件而不是字节。保存读取文件的步骤。