zhangruiyu / tencent_cos

Apache License 2.0
22 stars 10 forks source link

上传进度回调 #7

Closed hellohejinyu closed 2 years ago

hellohejinyu commented 2 years ago

Hello,作者你好👋可以加一个上传进度的回调嘛。

zhangruiyu commented 2 years ago

没做哎,而且我也用不到,都是小东西,你可以自己添加,没啥难度

iCytus commented 2 years ago
typedef ProgressCallback = Function(int totalByteCount, int completedByteCount);

  Future<String?> putObject(String objectKey, String filePath,
      {String? token,String? contentType = "image/jpeg"}) async {
    cosLog("putObject");
    var f = File(filePath);
    int flength = await f.length();
    var req = await getRequest("PUT", objectKey,
        headers: {
          "content-type": contentType,
          "content-length": flength.toString()
        },
        token: token);
    var fs = f.openRead();
   // 主要增加以下内容
    int byteCount = 0;
    Stream<List<int>> s = fs.transform(
      StreamTransformer.fromHandlers(
        handleData: (data, sink) {
          byteCount += data.length;
          if (callback != null) {
            callback!( flength, byteCount);
          }
          sink.add(data);
        },
        handleError: (error, stack, sink) {},
        handleDone: (sink) {
          sink.close();
        },
      ),
    );

    await req.addStream(s);
    var response = await req.close();
    cosLog("request-id:" + (response.headers["x-cos-request-id"]?.first ?? ""));
    if (response.statusCode != 200) {
      String content = await response.transform(utf8.decoder).join("");
      cosLog("putObject error content: $content");
      return null;
    } else {
      return objectKey;
    }
  }

这是我修改后的方法,希望对你有所帮助

hellohejinyu commented 2 years ago
typedef ProgressCallback = Function(int totalByteCount, int completedByteCount);

  Future<String?> putObject(String objectKey, String filePath,
      {String? token,String? contentType = "image/jpeg"}) async {
    cosLog("putObject");
    var f = File(filePath);
    int flength = await f.length();
    var req = await getRequest("PUT", objectKey,
        headers: {
          "content-type": contentType,
          "content-length": flength.toString()
        },
        token: token);
    var fs = f.openRead();
   // 主要增加以下内容
    int byteCount = 0;
    Stream<List<int>> s = fs.transform(
      StreamTransformer.fromHandlers(
        handleData: (data, sink) {
          byteCount += data.length;
          if (callback != null) {
            callback!( flength, byteCount);
          }
          sink.add(data);
        },
        handleError: (error, stack, sink) {},
        handleDone: (sink) {
          sink.close();
        },
      ),
    );

    await req.addStream(s);
    var response = await req.close();
    cosLog("request-id:" + (response.headers["x-cos-request-id"]?.first ?? ""));
    if (response.statusCode != 200) {
      String content = await response.transform(utf8.decoder).join("");
      cosLog("putObject error content: $content");
      return null;
    } else {
      return objectKey;
    }
  }

这是我修改后的方法,希望对你有所帮助

收到,谢谢!