How to calculate percent of downloading data from web service
I call a method from web service to get a binary data. The data's size is more big. How to get the percent of downloading data from web service? I use BackgroundWorker thread to get data asynchorously.
Thanks.
Update: This is the method from my web service
[WebMethod]
public byte[] Data()
{
byte[] buffer;
string filePath = Server.MapPath("Services.rar");
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
try
{
int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = 0; 开发者_如何学编程 // total number of bytes read
// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
}
finally
{
fileStream.Close();
}
return buffer;
}
This method will return a binary file. And my codes to get data from this method in WinForm application:
private void button1_Click(object sender, EventArgs e)
{
localhost.Service1 service = new localhost.Service1();
byte[] data = service.Data();
// where I get data, time to finish getting data depended on file's size.
// I want to calculate the time to finish and display percentage
}
Please help me. Thanks.
You can use soap extensions to do this.
This won't build but should give you the idea: webResponse.ContentLength; will give total size assuming the server sends it. Current progress can be kept track of. Use a delegate to update status in a ui thread.
private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);
private void Download(string url, string localPath)
{
using (WebClient wcDownload = new WebClient())
{
try
{
// Create a request to the file we are downloading
webRequest = (HttpWebRequest)WebRequest.Create(url);
// Set default authentication for retrieving the file
webRequest.Credentials = CredentialCache.DefaultCredentials;
// Retrieve the response from the server
webResponse = (HttpWebResponse)webRequest.GetResponse();
// Ask the server for the file size and store it
Int64 fileSize = webResponse.ContentLength;
// Open the URL for download
strResponse = wcDownload.OpenRead(txtUrl.Text);
// Create a new file stream where we will be saving the data (local drive)
strLocal = new FileStream(string localPath, FileMode.Create, FileAccess.Write, FileShare.None);
// It will store the current number of bytes we retrieved from the server
int bytesSize = 0;
// A buffer for storing and writing the data retrieved from the server
byte[] downBuffer = new byte[2048];
// Loop through the buffer until the buffer is empty
while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
// Write the data from the buffer to the local hard drive
strLocal.Write(downBuffer, 0, bytesSize);
// Invoke the method that updates the form's label and progress bar
this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, fileSize });
}
}
finally
{
// When the above code has ended, close the streams
strResponse.Close();
strLocal.Close();
}
}
private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
{
// Calculate the download progress in percentages
PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);
// Make progress on the progress bar
prgDownload.Value = PercentProgress;
// Display the current progress on the form
lblProgress.Text = "Downloaded " + BytesRead + " out of " + TotalBytes + " (" + PercentProgress + "%)";
if (BytesRead == TotalBytes)
{
//done
}
}
精彩评论