473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Downloading Files - weird problem

hi
I have a thread that downloades a file.
Problem is : Not all files are beeing downloaded. I observed that only the
small files are beeing downloaded correctly. I also cant download two files
in a row. I got to Exit and run the program again to even start downloading.
The second problem i interprett as iam not closing something ( but don't
know what ). For the first problem i have no idea what is wrong.Here is the
download thread
Maybe someone could help
try
{
HttpWebRequest request = null;
HttpWebResponse response = null;

request = (HttpWebRequest)WebRequest.Create(FullPath);
request.Headers.Clear();
request.ProtocolVersion = HttpVersion.Version11;

response = (HttpWebResponse)request.GetResponse();

FileStream = response.GetResponseStream();
// Download folder must work
file = new BinaryWriter(File.Create(@"d:\\FM\\" +
fileName));

while ((bufferCount = FileStream.Read(buffer, 0, 64000)) 0
&& !Stop)
{
//MessageBox.Show("STOP is: " + Stop.ToString() + "
BufferCount is: " + bufferCount.ToString());
store += bufferCount;
file.Write(buffer, 0, bufferCount);
}

file.Close();
response.Close();
FileStream.Close();
file.Close();

if (!Stop)
{
NormalExit = true;
exitDelegate myDeleg = new exitDelegate(exitForm);
this.Invoke(myDeleg);
}
}
Sep 17 '06 #1
4 1993
I found out that when i create instance of my Download class for a second
time the Close has the value from the first instance
Close is:

static bool stop;
static readonly object stopLock = new object();
static bool Stop
{
get
{
lock (stopLock)
{
return stop;
}
}
set
{
lock (stopLock)
{
stop = value;
}
}
}

Why does it happen? Shouldn't Dispose() clear the value of 'Close' ?
Sep 17 '06 #2
Hi Piotr,

There are a couple of issues that I can see here. First, you're not ensuring
that the Files you create are closed in the case of an exception:

file = new BinaryWriter(File.Create(@"d:\\FM\\" + fileName));

while ((bufferCount = FileStream.Read(buffer, 0, 64000)) 0 && !Stop)
{
//MessageBox.Show("STOP is: " + Stop.ToString() + "
BufferCount is: " + bufferCount.ToString());
store += bufferCount;
file.Write(buffer, 0, bufferCount);
}

file.Close();
response.Close();
FileStream.Close();
file.Close();

There are some things you can do to help this. Here is an example:
try
{
response = (HttpWebResponse)request.GetResponse();
using (file = new BinaryWriter(File.Create(@"d:\\FM\\" + fileName)))
{
while ((bufferCount = FileStream.Read(buffer, 0, 64000)) 0 &&
!Stop)
{
//MessageBox.Show("STOP is: " + Stop.ToString() + "
BufferCount is: " + bufferCount.ToString());
store += bufferCount;
file.Write(buffer, 0, bufferCount);
}
}
}
catch
{
// ... whatever exception-handling/recording you want here
}
finally
{
response.Close();
}

A couple of notes about the above: The "using" statement ensures that the
file is closed, as soon as the "using" block is exited. Also, note that I
closed the HttpWebResponse, but not the FileStream. This is because closing
the HttpWebResponse also closes the FileStream, although explicitly closing
the FileStream doesn't cause any problems; it's just unnecessary. I put the
"response.Close()" statement into a "finally" block, which means that it
will be executed no matter what happens.

Also, I'm not sure whether you may have some memory issues associated with
the allocation of such a large buffer, but that's not necessarily a problem.

The main thing is, you need to handle any possible exception, ensure that
IDisposable instances are disposed, and close the HttpWebResponse,
regardless of any exceptions.

Is this in a loop by any chance?

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.
"PiotrKolodziej" <pi*************@gmail.comwrote in message
news:bc***************************@news.chello.pl. ..
hi
I have a thread that downloades a file.
Problem is : Not all files are beeing downloaded. I observed that only the
small files are beeing downloaded correctly. I also cant download two
files in a row. I got to Exit and run the program again to even start
downloading. The second problem i interprett as iam not closing something
( but don't know what ). For the first problem i have no idea what is
wrong.Here is the download thread
Maybe someone could help
try
{
HttpWebRequest request = null;
HttpWebResponse response = null;

request = (HttpWebRequest)WebRequest.Create(FullPath);
request.Headers.Clear();
request.ProtocolVersion = HttpVersion.Version11;

response = (HttpWebResponse)request.GetResponse();

FileStream = response.GetResponseStream();
// Download folder must work
file = new BinaryWriter(File.Create(@"d:\\FM\\" +
fileName));

while ((bufferCount = FileStream.Read(buffer, 0, 64000)) >
0 && !Stop)
{
//MessageBox.Show("STOP is: " + Stop.ToString() + "
BufferCount is: " + bufferCount.ToString());
store += bufferCount;
file.Write(buffer, 0, bufferCount);
}

file.Close();
response.Close();
FileStream.Close();
file.Close();

if (!Stop)
{
NormalExit = true;
exitDelegate myDeleg = new exitDelegate(exitForm);
this.Invoke(myDeleg);
}
}

Sep 17 '06 #3
A couple of notes about the above: The "using" statement ensures that the
file is closed, as soon as the "using" block is exited.
Did you mean that if i use using directive i dont have to close file in ANY
situation
( normall exit or exception )?
closed the HttpWebResponse, but not the FileStream. This is because
closing the HttpWebResponse also closes the FileStream, although
explicitly closing the FileStream doesn't cause any problems; it's just
unnecessary. I put the "response.Close()" statement into a "finally"
block, which means that it will be executed no matter what happens.
Thank you for that.
>
Also, I'm not sure whether you may have some memory issues associated with
the allocation of such a large buffer, but that's not necessarily a
problem.
Everything is possible but this. I've Already tought this way.
>
The main thing is, you need to handle any possible exception, ensure that
IDisposable instances are disposed, and close the HttpWebResponse,
regardless of any exceptions.
This is only the beta version of the code. I have planned to put some try
blocks into the code later.
Anyway your 'thread' with using directive is valuable for me.
Sep 17 '06 #4
Did you mean that if i use using directive i dont have to close file in
ANY situation
( normall exit or exception )?
Yes. The using block is the same as a try/finally block.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"PiotrKolodziej" <pi*************@gmail.comwrote in message
news:a5***************************@news.chello.pl. ..
>
>A couple of notes about the above: The "using" statement ensures that the
file is closed, as soon as the "using" block is exited.

Did you mean that if i use using directive i dont have to close file in
ANY situation
( normall exit or exception )?
>closed the HttpWebResponse, but not the FileStream. This is because
closing the HttpWebResponse also closes the FileStream, although
explicitly closing the FileStream doesn't cause any problems; it's just
unnecessary. I put the "response.Close()" statement into a "finally"
block, which means that it will be executed no matter what happens.

Thank you for that.
>>
Also, I'm not sure whether you may have some memory issues associated
with the allocation of such a large buffer, but that's not necessarily a
problem.

Everything is possible but this. I've Already tought this way.
>>
The main thing is, you need to handle any possible exception, ensure that
IDisposable instances are disposed, and close the HttpWebResponse,
regardless of any exceptions.

This is only the beta version of the code. I have planned to put some try
blocks into the code later.
Anyway your 'thread' with using directive is valuable for me.


Sep 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: TJ | last post by:
Hi, I've written code web-based uploading and downloading. Here is some code for it. For saving file into MS-SQL database, SaveFileIntoDB(HttpPostedFile file) { int fileLength =...
2
by: Bala | last post by:
Hi I am trying to download the PDF files from my webserver using ASP.Net. All my files are stored at F Drive on webserver. Like this F:\Main Folder\Sub Folder\Files\File1.pdf I am...
4
by: Joe | last post by:
I'm hosting my web service on a Windows 2003 box which is remotely located. When trying to add a web reference to a C# project I get an error message 'There was an error downloading...
1
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
3
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
0
by: just.starting | last post by:
I am having problem while downloading files from an apache server2.0.53 with php4.3.10.While downloading some files it generally stops after downloading some specific amount and then stops...
6
by: Barry | last post by:
Hi all I have this script(download.php) which downloads binary data from a mysql database. <? /* SNIP */ $document=document::singleton();
0
by: kamal | last post by:
Hi, I have one problem . we have implemented overriding of IDownloadManager of Internet explorer. it was working fine. If we give any files which have direct hyperlink i.e from geocities or like...
7
by: Ehsan | last post by:
I foundd this code in ASPN Python Cookbook for downloading files in python but when it finished downloading files the files became corrupted and didn't open, the files in internet havn't any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.