颤振http请求上传mp3文件
我用这个api上传mp3文件 使用这种方法颤振http请求上传mp3文件,http,flutter,dart,upload,Http,Flutter,Dart,Upload,我用这个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
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);
}
Future uploadRecord(字符串匹配ID,字符串文件路径)异步{
uriurl=Uri.parse(
url.baseurl+EndPoints.uploadRecordEndPoint+'${auth.token}');
final request=http.MultipartRequest('POST',url)
..字段['match_id']=matchId
..files.add(http.MultipartFile.fromBytes(
“file”,等待file.fromUri(Uri(路径:filePath)).readAsBytes(),
contentType:MediaType('audio','mpeg');
最终响应=等待请求。发送();
final responsest=wait response.stream.bytesToString();
打印(应答器);
}
但它不工作,似乎没有上传文件,我是不是遗漏了什么?还是有更好的解决办法 请使用软件包上传文件
或者使用以下代码使用multipart上载文件:
static Future<String> fileUploadMultipart(
{File file, OnUploadProgressCallback onUploadProgress}) async {
assert(file != null);
final url = '$baseUrl/api/file';
final httpClient = getHttpClient();
final request = await httpClient.postUrl(Uri.parse(url));
int byteCount = 0;
var multipart = await http.MultipartFile.fromPath(fileUtil.basename(file.path), file.path);
// final fileStreamFile = file.openRead();
// var multipart = MultipartFile("file", fileStreamFile, file.lengthSync(),
// filename: fileUtil.basename(file.path));
var requestMultipart = http.MultipartRequest("", Uri.parse("uri"));
requestMultipart.files.add(multipart);
var msStream = requestMultipart.finalize();
var totalByteLength = requestMultipart.contentLength;
request.contentLength = totalByteLength;
request.headers.set(
HttpHeaders.contentTypeHeader, requestMultipart.headers[HttpHeaders.contentTypeHeader]);
Stream<List<int>> streamUpload = msStream.transform(
new StreamTransformer.fromHandlers(
handleData: (data, sink) {
sink.add(data);
byteCount += data.length;
if (onUploadProgress != null) {
onUploadProgress(byteCount, totalByteLength);
// CALL STATUS CALLBACK;
}
},
handleError: (error, stack, sink) {
throw error;
},
handleDone: (sink) {
sink.close();
// UPLOAD DONE;
},
),
);
await request.addStream(streamUpload);
final httpResponse = await request.close();
//
var statusCode = httpResponse.statusCode;
if (statusCode ~/ 100 != 2) {
throw Exception('Error uploading file, Status code: ${httpResponse.statusCode}');
} else {
return await readResponseAsString(httpResponse);
}
}
static Future fileUploadMultipart(
{File File,OnUploadProgressCallback onUploadProgress})异步{
断言(文件!=null);
最终url=“$baseUrl/api/file”;
最终httpClient=getHttpClient();
final request=wait-httpClient.postrl(Uri.parse(url));
int字节数=0;
var multipart=wait http.MultipartFile.fromPath(fileUtil.basename(file.path),file.path);
//final fileStreamFile=file.openRead();
//var multipart=MultipartFile(“file”,fileStreamFile,file.lengthSync(),
//文件名:fileUtil.basename(file.path));
var requestMultipart=http.MultipartRequest(“”,Uri.parse(“Uri”);
requestMultipart.files.add(多部分);
var msStream=requestMultipart.finalize();
var totalbytellength=requestMultipart.contentLength;
request.contentLength=totalbytellength;
request.headers.set(
HttpHeaders.contentTypeHeader,requestMultipart.headers[HttpHeaders.contentTypeHeader]);
streamUpload=msStream.transform(
新StreamTransformer.fromHandlers(
handleData:(数据,接收器){
增加(数据);
字节计数+=data.length;
if(onUploadProgress!=null){
onUploadProgress(字节数、总字节长度);
//呼叫状态回调;
}
},
handleError:(错误、堆栈、接收器){
投掷误差;
},
把手:(水槽){
sink.close();
//上传完成;
},
),
);
等待请求。addStream(streamUpload);
final httpResponse=等待请求。关闭();
//
var statusCode=httpResponse.statusCode;
如果(状态代码~/100!=2){
抛出异常('上载文件时出错,状态代码:${httpResponse.statusCode}');
}否则{
返回等待读取响应字符串(httpResponse);
}
}
尝试将文件名添加到
http.MultipartFile.fromBytes()
这不会解决您的问题,但您应该使用另一个命名的MultipartFile构造函数,它接受一个文件而不是字节。保存读取文件的步骤。