File download in Chunks

  • Thread starter Thread starter want 2 Learn
  • Start date Start date
W

want 2 Learn

Guest
i am trying to download a file from

Error code 400 while downloading a file via REST API

in the page there is pythin code that uses Range header to do so.

i tried to implement it in C#, but i only get the beginning of the file over and over :

static private void download_file(string filename, string token, int fileSize, int chunkNumber, long totalBytesRead)
{
string run_date = DateTime.Now.ToString("MMddyyyy").ToString();
HttpManager httpManager = new HttpManager();
string filePath = $"c://Temp//{filename}.xml";
if (chunkNumber == 0 && File.Exists(filePath))
File.Delete(filePath);
string url = "https://" + ip_address + "/mgmt/tm/asm/file-transfer/downloads/" + filename + ".xml";
try
{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "GET";
try
{
httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "GET";
//httpRequest.AllowReadStreamBuffering = false;
httpRequest.Headers.Add("X-F5-Auth-Token", token);
if (chunkNumber > 0) {
int endRange = (chunkSize * (chunkNumber + 1)) - 1;
if (endRange > fileSize) endRange = fileSize;
httpRequest.AddRange(chunkSize * chunkNumber, endRange);
}
using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
{
using (Stream responseStream = httpResponse.GetResponseStream())
{
using (FileStream localFileStream =
new FileStream(filePath, FileMode.Append))
{
var buffer = new byte[4096];

int bytesRead;
int totalBytesRead_ = 0;
while (totalBytesRead_ < httpResponse.ContentLength )
{
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
totalBytesRead_ += bytesRead;
localFileStream.Write(buffer, 0, bytesRead);
}
}
}
}
}
catch (Exception ex)
{
throw;
}
}
catch (Exception ex)
{
throw;
}
if (fileSize > totalBytesRead * (chunkNumber+1))
download_file(filename, token, fileSize, ++chunkNumber, totalBytesRead);

}//end of download_file
}


what can be wrong?

Continue reading...
 

Similar threads

Back
Top