Skip to content

How to upload by slice #63

Description

@iewnfod

I tried to upload a large file by slice and combine them after uploaded. After upload, there should be some functions like move to combine those segments, but I cannot find it.

Here is part of my code to upload.

Future<void> _uploadByChunk(Client davClient) async {
  final fileSize = getFileSize(localPath);
  final sliceCount = (fileSize / UPLOAD_SLICE_SIZE).ceil();

  final f = io.File(localPath);

  setStatus(.inProgress);

  for (int i = _uploadedSlice ?? 0; i < sliceCount; i++) {
    if (_cancelToken?.isCancelled == true) {
      print("Upload $id cancelled, stopping chunked upload");
      return;
    }
    final offset = i * UPLOAD_SLICE_SIZE;
    final currentChunkSize = (i == sliceCount - 1)
        ? fileSize - offset
        : UPLOAD_SLICE_SIZE;
    final sliceStream = await f
        .openRead(offset, offset + currentChunkSize)
        .toList();
    final sliceData = sliceStream.expand((x) => x).toList();

    await davClient.write(
      "$remotePath.part$i",
      Uint8List.fromList(sliceData),
      onProgress: (count, total) {
        final overallProgress = ((i * UPLOAD_SLICE_SIZE) + count) / fileSize;
        setProgress((overallProgress * fileSize).toInt(), fileSize);
      },
      cancelToken: _cancelToken,
    );

    _uploadedSlice = i + 1;
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions