I'm not familiar with that kind of chunked upload, but I believe this should work:
- Use
flowTotalSize
to pre-allocate the file when the first chunk comes in. - Have one
SemaphoreSlim
per file to serialize the asynchronous writes for that file. - Each chunk will write to its own offset (
flowChunkSize * (flowChunkNumber - 1)
) within the file.
This doesn't handle situations where the uploads are unexpectedly terminated. That kind of solution usually involves allocating/writing a temporary file (with a special extension) and then moving/renaming that file once the last chunk arrives.
Don't forget to ensure that your file writing is actually asynchronous.