There are a lot of missing information that makes it hard to help you. But here are some answers based on random guesses on what you might need.
If you need to post to an API using the HTTP chunked transport, simply open the source file as a stream, then use
StreamContent with an
HttpClient.
If the "chunked" refers to the API expecting the data to be uploaded using multiple POST calls, each call transferring a subset of the data, then there are a few options:
If memory usage (and performance) is not a big concern this produce reasonable readable code:
1) Open a file stream
2) Create a
BinaryReader for the stream
3) Read chunksize bytes from the BinaryReader.
4) Post the data to the API
5) Go to step 3) if not completed.
If memory usage is still a problem, ensure you allocate a single byte array with the required chunksize and use the
BinaryReader.Read(byte[] buffer, int index, int count)
method in step 3). You can even do that on the Stream itself (so bypassing the BinaryReader), but then you need to understand that Stream.Read does not always read the number of bytes you expect - forcing you to write an extra loop. You can as well let the BinaryReader do that for you.
If memory/performance use is a concern, then you can implement your own "chunk stream". It is not extremely difficult, but still - there is always room for mistakes :). Maybe a reasonable nuget package is available so you do not need to implement it yourself, I don't know.
Making a custom stream would allow the data to be send through with only small parts placed in memory. It will also ensure reading the file and posting the data happens concurrently further increasing performance.
To implement it:
Make a class called something meaningful (the most difficult part is to have a decent name). FileChunkStream or something should do.
Add a constructor that accepts a Stream (which will be the FileStream you open) and the chunk size).
Add the base class Stream
Implement all the required methods (you can throw a NotSupportedException on Write operations), by calling the base stream you got in the constructor, making the necessary offset calculations for Length, Seek and Position based on the chunk size and index. For the length, remember to deal with the last chunk correctly, it will most likely NOT be the same length as the rest.
You can't have multiple chunks for the same Stream open at the same time with the most basic implementation as it all chunks would use the current Position of the underlying Stream. Two options are available if this is needed: Let each FileChunkStream track it's own position - and seek before any read operation. You would need to protect with a semaphore or something, so if you are not an experienced programmer, I would instead recommend you simply open the same file multiple times (once for each chunk). Open the file for Read only and specify FileShare.Read.
The details I gave above also assumes the stream is in the right position for the next chunk when you open it. You can also add the chunk index to the constructor - then the position can easily be calculated.
I do realize that this is a very high level description, but writing it at this level is already taking a lot of time, and I do not even know if you need it. So if you do need this, and need more details, please ask SPECIFIC questions.