I implemented it in the following way currently. I would like to know if there is any better approach than this?
i don't see any problem in what you have implemented in terms of your client should be able to do other tasks (here to call other APIs) while downloading is going on.
But as side-note, i would suggest not to download whole file in one go.
if you are doing it in such way, your one and only request will be too much important for you to be successfully responded. As if your downloading is going on and network goes out, whole downloading will be failed.
Instead of this,
- (if you not have it already) you make whole content in byte[] (there are multiple ways to do that)
- divide whole byte[] into number of chunks (with reasonable size)
- Download them one by one (or even parallel if you have liberty with broadband)
- Merge them togather at client side
- And keep the note till which chunk you have already downloaded data (and keep updating this information with every response)
this way you downloading process will have feature of Pause-Resume, and it will be scalable and robust for fail condition (as after network issue resolves you will need to download only remaining data only)
You can devide and merge byte[]
like below
here list operations are easy to be done so converting array into list first
//Suppose your main byte[] is array
List<byte> mainInputList = array.ToList();
Now dividing whole thing into multiple chunks.
//making chunks (size of 10)
List<List<byte>> listOfChunks = new List<List<byte>>();
List<byte> chunks = new List<byte>();
for (int i = 0; i < mainInputList.Count; i++)
{
chunks.Add(array[i]);
if (chunks.Count == 10)
{
listOfChunks.Add(chunks);
chunks = new List<byte>();
}
}
if (chunks.Count > 0)
listOfChunks.Add(chunks);
Now merging them back.
//Merging Chunks
List<byte> finalByteList = new List<byte>();
foreach(List<byte> chunk in listOfChunks)
{
finalByteList.AddRange(chunk);
}
byte[] finalByteArr = finalByteList.ToArray();
for making byte[] form any object and making object from byte[]:
How to convert byte array to any type
Update
Quoting an important point which is raised in comment section.
Keep in mind:
If you need to chunk downloads they are obviously of such a large size that I wouldn’t want to store them in memory if they finally get written to the disk anyway. Just for each chunk (which should have a fixed or at least max size) you read, seek to the file position where it belongs and write it. This way you can download arbitrarily large files and are not bounded by memory.