upload static method

Future<String> upload({
  1. required File file,
  2. Function? cancelToken,
  3. Function? onSendProgress,
})

@params file 要上传的文件对象 @param callback 回调函数用于传cancelToken,方便后期关闭请求 @param onSendProgress 上传的进度事件

Implementation

static Future<String> upload(
    {required File file,
    Function? cancelToken,
    Function? onSendProgress}) async {
  final res = await ComConfig.getOssConfig();
  // String policyText = '{"expiration": "$expiration","conditions": [{"bucket": "$bucket" },["content-length-range", 0, 1048576000]]}';
  /// 获取签名
  // String signature = getSignature(policyText);

  /// 生成oss的路径和文件名目前设置的是images/20201229/test.mp4
  String pathName =
      '${res["dir"] ?? "images"}/${getDate()}/${getRandom(12)}.${getFileType(file.path)}';

  /// 请求参数的form对象
  FormData data = FormData.fromMap({
    'key': pathName,
    'policy': res["policy"],
    // 'policy': getPolicyBase64(policyText),
    'OSSAccessKeyId': res["accessKeyId"],
    // 'OSSAccessKeyId': accessKeyId,
    'success_action_status': '200', //让服务端返回200,不然,默认会返回204
    'signature': res["signature"],
    // 'signature': signature,
    'contentType': 'multipart/form-data',
    'file': MultipartFile.fromFileSync(file.path),
  });

  try {
    /// 发送请求
    var result = await RequestClient.instance.request(
      res["url"],
      method: HttpMethod.post.name,
      data: data,
    );

    /// 成功后返回文件访问路径
    if (res["url"].contains("http")) {
      return '${res["url"]}/$pathName';
    } else {
      return result;
    }
  } catch (e) {
    throw (e.toString());
  }
}