问题描述
我正在尝试将大于 100mb 的视频文件分块上传到 cloudinary。
import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'package:http/http.dart' as http;
import 'network.dart';
class ChunkedUploader {
final http.Client _client;
ChunkedUploader(this._client);
Future<http.StreamedResponse> upload({
String url,File file,Map<String,dynamic> body,int maxChunkSize,int uniqueId,Function(double) onUploadProgress,}) =>
UploadRequest(
_client,url: url,file: file,body: body,maxChunkSize: maxChunkSize,uniqueId: uniqueId,onUploadProgress: onUploadProgress,).upload();
}
class UploadRequest {
final http.Client client;
final String url;
final File file;
final Map<String,dynamic> body;
final int uniqueId;
final Function(double) onUploadProgress;
int _fileSize;
int _maxChunkSize;
UploadRequest(
this.client,{
this.url,this.file,this.body,this.uniqueId,this.onUploadProgress,}) {
_fileSize = file.lengthSync();
_maxChunkSize = min(_fileSize,maxChunkSize ?? _fileSize);
}
Future<http.StreamedResponse> upload() async {
http.StreamedResponse finalResponse;
for (int i = 0; i < _chunksCount; i++) {
final start = _getChunkStart(i);
final end = _getChunkEnd(i);
final chunkStream = _getChunkStream(start,end);
MultipartRequest request = MultipartRequest(
"POST",Uri.parse(url),onProgress: (int bytes,int total) {
_updateProgress(i,bytes,total);
},);
//Add the headers
request.headers.addAll(_getHeaders(start,end));
//Add the body
request.fields.addAll(body);
//Get the file
http.multipartfile multipartfile =
http.multipartfile("file",chunkStream,end - start);
request.files.add(multipartfile);
//Send the file
http.StreamedResponse response =
await client.send(request).catchError((error) {
throw error;
});
print(response);
finalResponse = response;
}
return finalResponse;
}
Stream<List<int>> _getChunkStream(int start,int end) =>
file.openRead(start,end);
// Updating total upload progress
_updateProgress(int chunkIndex,int chunkCurrent,int chunkTotal) {
int totalUploadedSize = (chunkIndex * _maxChunkSize) + chunkCurrent;
double totalUploadProgress = (totalUploadedSize / _fileSize) * 100;
this.onUploadProgress?.call(totalUploadProgress);
}
// Returning start byte offset of current chunk
int _getChunkStart(int chunkIndex) => chunkIndex * _maxChunkSize;
// Returning end byte offset of current chunk
int _getChunkEnd(int chunkIndex) =>
min((chunkIndex + 1) * _maxChunkSize,_fileSize);
// Returning a header map object containing Content-Range
// https://tools.ietf.org/html/rfc7233#section-2
Map<String,String> _getHeaders(int start,int end) => {
'X-Unique-Upload-Id': "$uniqueId",'Content-Range': 'bytes $start-${end - 1}/$_fileSize',};
// Returning chunks count based on file size and maximum chunk size
int get _chunksCount => (_fileSize / _maxChunkSize).ceil();
}
在提出请求时,我收到了
的回复Status code - 500
Body - {
“error” : {
“message” : "General Error”
}
}
我已与 cloudinary 取得联系,他们已找到该请求,并表示该请求失败并显示错误:UTF-8 中的字节序列无效。
任何帮助将不胜感激
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)