huhx / flutter_oss_aliyun

阿里云oss sdk的flutter版本
MIT License
61 stars 22 forks source link

上传文件支持中途取消操作吗? #25

Closed LIYONGBIN123456 closed 1 year ago

LIYONGBIN123456 commented 1 year ago

亲爱的作者你好,请问在上传文件的过程中支持中途取消操作吗?如果不支持,我该怎么中途取消上传文件操作呢

huhx commented 1 year ago

在考虑cancel的功能,可以了解一下你这边在上传过程需要取消的场景吗?有简单的代码最好

LIYONGBIN123456 commented 1 year ago

在考虑cancel的功能,可以了解一下你这边在上传过程需要取消的场景吗?有简单的代码最好

是的上传过程中有取消的操作,代码是在项目里面的,耦合的东西比较多,我试试看能不能抽出来

huhx commented 1 year ago

我写了些测试样例代码:

final CancelToken cancelToken = CancelToken();

final File file = File("$home/Downloads/idiom.csv");
final String string = file.readAsStringSync();

await Client().putObject(
  Uint8List.fromList(utf8.encode(string)),
  "cancel_token_test.csv",
  option: PutRequestOption(
    onSendProgress: (count, total) {
      if (kDebugMode) {
        print("send: count = $count, and total = $total");
      }
      if (count > 56) {
        cancelToken.cancel("cancel the uploading.");
      }
    },
  ),
  cancelToken: cancelToken,
).then((response) {
  // success
  print("upload success = ${response.statusCode}");
}).catchError((err) {
  if (CancelToken.isCancel(err)) {
    print("error message = ${err.message}");
  } else {
    // handle other errors
  }
});

代码中在上传bytes > 56时,cancel上传。cancelToken.cancel这个方法会throw DioError.cancel,然后在catchError中,使用if (CancelToken.isCancel(err))判断error是否来自于cancel的,可以对这种特定的error做处理。

不知道这个满不满足你的场景

huhx commented 1 year ago

准备在所有的api添加一个可选的参数CancelToken,当调用CancelToken里面的cancel方法,会取消request的请求。

LIYONGBIN123456 commented 1 year ago

应该能满足基本需求了,后面测试一下,如果出现问题,继续给你提issue。非常感谢

huhx commented 1 year ago

^5.1.4,所有api都添加了可选的参数CancelToken,用于request的cancel。