Dear all
I'm trying to cancel a downloading operation. My scenario is as follows:
When the user clicks on Cancel Download Button so this action throws exception in Download function which is as follows:
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + uri + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.UsePassive = true;
response = (FtpWebResponse)reqFTP.GetResponse();
ftpStream = response.GetResponseStream();
_isItOutputStream = true;
string dataLengthString = response.Headers["Content-Length"];
int dataLength = 0;
if (dataLengthString != null)
{
dataLength = Convert.ToInt32(dataLengthString);
}
long cl = response.ContentLength;
int bufferSize = 4048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
bool first = true;
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
_actualDownloaded += readCount;
if (this.InvokeRequired)
{
ProgressBarDel _progressDel = new ProgressBarDel(ProgressBar);
this.Invoke(_progressDel, new object[] { _actualDownloaded, first });
}
first = false;
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
_isItOutputStream = false;
return true;
}
catch (Exception ee)
{
_downloadException = ee.Message;
if (response != null)
{
outputStream.Close();
ftpStream.Close();
response.Close();
}
return false;
}
In the line " ftpStream.Close()
" here where the exception is thrown...
The exception text is:
The remote server returned an error: (450) File unavailable (e.g., file busy)
where it opens a file to download as what i wrote " outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
" i wont to close this stream and to close the response in order if the user did as follows:
download -> cancel download -> download -> cancel download -> download
if this scenario happened the application hungs up. i don't know how to close the stream and the response so i can stop downloading then delete the created file in order to download again.
thnx