Webclient returning download progress as 0
I have written a helper class to download file from a server using the webclient class. The code was working fine until a while ago. All of a sudden the code is not working, the download progress is always 0 and toal bytes to receive is always -1. The download works fine but the issue is only with the actual progress change. The download progress remains zero from start to the end of download process, even the total bytes to receive remain as -1.
Please find the sample code mentioned below.
public class FileDownloader
{
private string _zipFilePath;
private string _destinationPath;
private int _productId;
public Action<int,int> progressListener;
private WebClient _client;
public void Download(string cloudPath, string localPath,int id)
{
_productId = id;
_zipFilePath = localPath +id+ ".zip";
_destinationPath = localPath + id;
if (!Directory.Exists(localPath))
{
Directory.CreateDirectory(localPath);
}
_client = new WebClient();
if (cloudPath != "")
{
Uri uri = new Uri(cloudPath);
_client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCallback);
_client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
_client.DownloadFileAsync(uri, _zipFilePath);
}else
{
progressListener?.Invoke(_productId, -1);
}
}
public void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
if(e.ProgressPercentage != 100)
progressListener?.Invoke(_productId, e.ProgressPercentage);
}
public void CancelDownload()
{
_client.CancelAsync();
}
public void DownloadFileCallback(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled == false && e.Error == null)
{
//UnzipContent(_destinationPath,_zipFilePath);
UnzipHandler unzipHandler = new UnzipHandler(_destinationPath, _zipFilePath,new UnzipCompletionCallback(UnzippedCourse));
Thread thread = new Thread(new ThreadStart(unzipHandler.UnzipContent));
thread.Start();
}
else
{
DeleteZip(_zipFilePath);
progressListener?.Invoke(_productId, -1);
}
}
private void DeleteZip(string zipPath)
{
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
}
public void UnzippedCourse(bool status)
{
DeleteZip(_zipFilePath);
if(status)
progressListener?.Invoke(_productId, 100);
else
progressListener?.Invoke(_productId, -1);
}
public delegate void UnzipCompletionCallback(bool result);
}
xamarin xamarin.ios xamarin.android webclient webclient-download
add a comment |
I have written a helper class to download file from a server using the webclient class. The code was working fine until a while ago. All of a sudden the code is not working, the download progress is always 0 and toal bytes to receive is always -1. The download works fine but the issue is only with the actual progress change. The download progress remains zero from start to the end of download process, even the total bytes to receive remain as -1.
Please find the sample code mentioned below.
public class FileDownloader
{
private string _zipFilePath;
private string _destinationPath;
private int _productId;
public Action<int,int> progressListener;
private WebClient _client;
public void Download(string cloudPath, string localPath,int id)
{
_productId = id;
_zipFilePath = localPath +id+ ".zip";
_destinationPath = localPath + id;
if (!Directory.Exists(localPath))
{
Directory.CreateDirectory(localPath);
}
_client = new WebClient();
if (cloudPath != "")
{
Uri uri = new Uri(cloudPath);
_client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCallback);
_client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
_client.DownloadFileAsync(uri, _zipFilePath);
}else
{
progressListener?.Invoke(_productId, -1);
}
}
public void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
if(e.ProgressPercentage != 100)
progressListener?.Invoke(_productId, e.ProgressPercentage);
}
public void CancelDownload()
{
_client.CancelAsync();
}
public void DownloadFileCallback(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled == false && e.Error == null)
{
//UnzipContent(_destinationPath,_zipFilePath);
UnzipHandler unzipHandler = new UnzipHandler(_destinationPath, _zipFilePath,new UnzipCompletionCallback(UnzippedCourse));
Thread thread = new Thread(new ThreadStart(unzipHandler.UnzipContent));
thread.Start();
}
else
{
DeleteZip(_zipFilePath);
progressListener?.Invoke(_productId, -1);
}
}
private void DeleteZip(string zipPath)
{
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
}
public void UnzippedCourse(bool status)
{
DeleteZip(_zipFilePath);
if(status)
progressListener?.Invoke(_productId, 100);
else
progressListener?.Invoke(_productId, -1);
}
public delegate void UnzipCompletionCallback(bool result);
}
xamarin xamarin.ios xamarin.android webclient webclient-download
stackoverflow.com/a/31256247/4984832
– SushiHangover
Nov 21 '18 at 11:46
But the same code was working previously. Nothing seems to have changed from any side.
– Midhun Kumar
Nov 23 '18 at 10:55
Look at the returned headers and see if the content length is zero...
– SushiHangover
Nov 23 '18 at 14:51
@SushiHangover I checked the response header and it has content length property in it.
– Midhun Kumar
Nov 26 '18 at 9:27
@SushiHangover also the content type was application/octet-stream
– Midhun Kumar
Nov 26 '18 at 9:33
add a comment |
I have written a helper class to download file from a server using the webclient class. The code was working fine until a while ago. All of a sudden the code is not working, the download progress is always 0 and toal bytes to receive is always -1. The download works fine but the issue is only with the actual progress change. The download progress remains zero from start to the end of download process, even the total bytes to receive remain as -1.
Please find the sample code mentioned below.
public class FileDownloader
{
private string _zipFilePath;
private string _destinationPath;
private int _productId;
public Action<int,int> progressListener;
private WebClient _client;
public void Download(string cloudPath, string localPath,int id)
{
_productId = id;
_zipFilePath = localPath +id+ ".zip";
_destinationPath = localPath + id;
if (!Directory.Exists(localPath))
{
Directory.CreateDirectory(localPath);
}
_client = new WebClient();
if (cloudPath != "")
{
Uri uri = new Uri(cloudPath);
_client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCallback);
_client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
_client.DownloadFileAsync(uri, _zipFilePath);
}else
{
progressListener?.Invoke(_productId, -1);
}
}
public void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
if(e.ProgressPercentage != 100)
progressListener?.Invoke(_productId, e.ProgressPercentage);
}
public void CancelDownload()
{
_client.CancelAsync();
}
public void DownloadFileCallback(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled == false && e.Error == null)
{
//UnzipContent(_destinationPath,_zipFilePath);
UnzipHandler unzipHandler = new UnzipHandler(_destinationPath, _zipFilePath,new UnzipCompletionCallback(UnzippedCourse));
Thread thread = new Thread(new ThreadStart(unzipHandler.UnzipContent));
thread.Start();
}
else
{
DeleteZip(_zipFilePath);
progressListener?.Invoke(_productId, -1);
}
}
private void DeleteZip(string zipPath)
{
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
}
public void UnzippedCourse(bool status)
{
DeleteZip(_zipFilePath);
if(status)
progressListener?.Invoke(_productId, 100);
else
progressListener?.Invoke(_productId, -1);
}
public delegate void UnzipCompletionCallback(bool result);
}
xamarin xamarin.ios xamarin.android webclient webclient-download
I have written a helper class to download file from a server using the webclient class. The code was working fine until a while ago. All of a sudden the code is not working, the download progress is always 0 and toal bytes to receive is always -1. The download works fine but the issue is only with the actual progress change. The download progress remains zero from start to the end of download process, even the total bytes to receive remain as -1.
Please find the sample code mentioned below.
public class FileDownloader
{
private string _zipFilePath;
private string _destinationPath;
private int _productId;
public Action<int,int> progressListener;
private WebClient _client;
public void Download(string cloudPath, string localPath,int id)
{
_productId = id;
_zipFilePath = localPath +id+ ".zip";
_destinationPath = localPath + id;
if (!Directory.Exists(localPath))
{
Directory.CreateDirectory(localPath);
}
_client = new WebClient();
if (cloudPath != "")
{
Uri uri = new Uri(cloudPath);
_client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCallback);
_client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
_client.DownloadFileAsync(uri, _zipFilePath);
}else
{
progressListener?.Invoke(_productId, -1);
}
}
public void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
if(e.ProgressPercentage != 100)
progressListener?.Invoke(_productId, e.ProgressPercentage);
}
public void CancelDownload()
{
_client.CancelAsync();
}
public void DownloadFileCallback(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled == false && e.Error == null)
{
//UnzipContent(_destinationPath,_zipFilePath);
UnzipHandler unzipHandler = new UnzipHandler(_destinationPath, _zipFilePath,new UnzipCompletionCallback(UnzippedCourse));
Thread thread = new Thread(new ThreadStart(unzipHandler.UnzipContent));
thread.Start();
}
else
{
DeleteZip(_zipFilePath);
progressListener?.Invoke(_productId, -1);
}
}
private void DeleteZip(string zipPath)
{
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
}
public void UnzippedCourse(bool status)
{
DeleteZip(_zipFilePath);
if(status)
progressListener?.Invoke(_productId, 100);
else
progressListener?.Invoke(_productId, -1);
}
public delegate void UnzipCompletionCallback(bool result);
}
xamarin xamarin.ios xamarin.android webclient webclient-download
xamarin xamarin.ios xamarin.android webclient webclient-download
asked Nov 21 '18 at 11:38
Midhun KumarMidhun Kumar
11211
11211
stackoverflow.com/a/31256247/4984832
– SushiHangover
Nov 21 '18 at 11:46
But the same code was working previously. Nothing seems to have changed from any side.
– Midhun Kumar
Nov 23 '18 at 10:55
Look at the returned headers and see if the content length is zero...
– SushiHangover
Nov 23 '18 at 14:51
@SushiHangover I checked the response header and it has content length property in it.
– Midhun Kumar
Nov 26 '18 at 9:27
@SushiHangover also the content type was application/octet-stream
– Midhun Kumar
Nov 26 '18 at 9:33
add a comment |
stackoverflow.com/a/31256247/4984832
– SushiHangover
Nov 21 '18 at 11:46
But the same code was working previously. Nothing seems to have changed from any side.
– Midhun Kumar
Nov 23 '18 at 10:55
Look at the returned headers and see if the content length is zero...
– SushiHangover
Nov 23 '18 at 14:51
@SushiHangover I checked the response header and it has content length property in it.
– Midhun Kumar
Nov 26 '18 at 9:27
@SushiHangover also the content type was application/octet-stream
– Midhun Kumar
Nov 26 '18 at 9:33
stackoverflow.com/a/31256247/4984832
– SushiHangover
Nov 21 '18 at 11:46
stackoverflow.com/a/31256247/4984832
– SushiHangover
Nov 21 '18 at 11:46
But the same code was working previously. Nothing seems to have changed from any side.
– Midhun Kumar
Nov 23 '18 at 10:55
But the same code was working previously. Nothing seems to have changed from any side.
– Midhun Kumar
Nov 23 '18 at 10:55
Look at the returned headers and see if the content length is zero...
– SushiHangover
Nov 23 '18 at 14:51
Look at the returned headers and see if the content length is zero...
– SushiHangover
Nov 23 '18 at 14:51
@SushiHangover I checked the response header and it has content length property in it.
– Midhun Kumar
Nov 26 '18 at 9:27
@SushiHangover I checked the response header and it has content length property in it.
– Midhun Kumar
Nov 26 '18 at 9:27
@SushiHangover also the content type was application/octet-stream
– Midhun Kumar
Nov 26 '18 at 9:33
@SushiHangover also the content type was application/octet-stream
– Midhun Kumar
Nov 26 '18 at 9:33
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411263%2fwebclient-returning-download-progress-as-0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411263%2fwebclient-returning-download-progress-as-0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
stackoverflow.com/a/31256247/4984832
– SushiHangover
Nov 21 '18 at 11:46
But the same code was working previously. Nothing seems to have changed from any side.
– Midhun Kumar
Nov 23 '18 at 10:55
Look at the returned headers and see if the content length is zero...
– SushiHangover
Nov 23 '18 at 14:51
@SushiHangover I checked the response header and it has content length property in it.
– Midhun Kumar
Nov 26 '18 at 9:27
@SushiHangover also the content type was application/octet-stream
– Midhun Kumar
Nov 26 '18 at 9:33