Please help coding the Progress bar when downloading a file :((

SuperShinta

Member
Joined
Oct 8, 2011
Messages
12
Programming Experience
Beginner
Imports System.Net
Imports System.IO
Public Class Form4


Private Sub listFTP(ByVal URL As String, ByVal UserName As String, ByVal Password As String)


Dim requ As FtpWebRequest = Nothing
Dim resp As FtpWebResponse = Nothing
Dim reader As StreamReader = Nothing
Try
requ = CType(WebRequest.Create(URL), WebRequest)
requ.Credentials = New NetworkCredential(UserName, Password)
requ.Method = WebRequestMethods.Ftp.ListDirectory
resp = CType(requ.GetResponse(), FtpWebResponse)
reader = New StreamReader(resp.GetResponseStream())
While (reader.Peek() > -1)
lstFileList.Items.Add(reader.ReadLine())
End While
ToolStripStatusLabel1.Text = "Listing complete!"
Catch ex As UriFormatException
ToolStripStatusLabel1.Text = ex.Message
Catch ex As WebException
ToolStripStatusLabel2.Text = ex.Message
Finally
If reader IsNot Nothing Then reader.Close()
End Try
End Sub


Private Sub downloadFTP(ByVal URL As String, ByVal UserName As String, ByVal Password As String)




Dim requ As FtpWebRequest = Nothing
Dim resp As FtpWebResponse = Nothing
Dim respStrm As Stream = Nothing
Dim fileStrm As FileStream = Nothing


ProgressBar1.Value = 0
ProgressBar1.Maximum = 100


Try
requ = CType(WebRequest.Create(URL), FtpWebRequest)
requ.Credentials = New NetworkCredential(UserName, Password)
requ.Method = WebRequestMethods.Ftp.DownloadFile
resp = CType(requ.GetResponse(), FtpWebResponse)
respStrm = resp.GetResponseStream()
SaveFileDialog1.FileName = Path.GetFileName(requ.RequestUri.LocalPath)
If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
fileStrm = File.Create(SaveFileDialog1.FileName)
Dim buff(1024) As Byte
Dim bytesRead As Integer = 0
While (True)
bytesRead = respStrm.Read(buff, 0, buff.Length)
If (bytesRead = 0) Then Exit While
fileStrm.Write(buff, 0, bytesRead)
End While
ToolStripStatusLabel1.Text = "Download complete!"
End If
Catch ex As UriFormatException
ToolStripStatusLabel1.Text = ex.Message
Catch ex As WebException
ToolStripStatusLabel2.Text = ex.Message
Catch ex As IOException
ToolStripStatusLabel2.Text = ex.Message
Finally
If respStrm IsNot Nothing Then respStrm.Close()
If fileStrm IsNot Nothing Then fileStrm.Close()
End Try
End Sub


Private Sub cmdList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdList.Click
lstFileList.Items.Clear()
listFTP(txturl.Text, txtusername.Text, txtpassword.Text)
End Sub


Private Sub cmdDowload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDownload.Click
downloadFTP(TextBox1.Text, txtusername.Text, txtpassword.Text)

End Sub


Private Sub lstFileList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstFileList.SelectedIndexChanged
TextBox1.Text = txturl.Text & "/" & lstFileList.SelectedItems(0).ToString()
End Sub


Private Sub deleteFTP(ByVal URL As String, ByVal UserName As String, ByVal Password As String)
Dim requ As FtpWebRequest = Nothing
Dim resp As FtpWebResponse = Nothing
Try
requ = CType(WebRequest.Create(URL), FtpWebRequest)
requ.Credentials = New NetworkCredential(UserName, Password)
requ.Method = WebRequestMethods.Ftp.DeleteFile
resp = CType(requ.GetResponse(), FtpWebResponse)
ToolStripStatusLabel1.Text = "File deleted!"
Catch ex As UriFormatException
ToolStripStatusLabel1.Text = ex.Message
Catch ex As WebException
ToolStripStatusLabel2.Text = ex.Message
Finally
If resp IsNot Nothing Then resp.Close()
End Try
End Sub

Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
If (MessageBox.Show("Are you sure you want to delete this file?", "Confirm File Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then
deleteFTP(TextBox1.Text, txtusername.Text, txtpassword.Text)
End If
End Sub


Private Sub cmdref_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdref.Click
lstFileList.Items.Clear()
listFTP(txturl.Text, txtusername.Text, txtpassword.Text)
End Sub

End Class

Thanks in advance :)
 
What seems to be the problem? You didn't give any sort of explanation as to what you need help with.

I'm sorry, uhmm. i wanna know what is the code for progress bar for download and where should i replace it. I searched for a 2 weeks but no luck. This is urgent please help. Thanks in advance :)
 
you could do this...

VB.NET:
Dim LngBytesRead As Long

While (True)
     bytesRead = respStrm.Read(buff, 0, buff.Length)
     If (bytesRead = 0) Then Exit While
     fileStrm.Write(buff, 0, bytesRead)

     LngBytesRead += CLng(bytesRead)
     ProgressBar1.Value = CInt((LngBytesRead / respStrm.Length) * 100)
End While

Should work.
 
you could do this...

VB.NET:
Dim LngBytesRead As Long

While (True)
     bytesRead = respStrm.Read(buff, 0, buff.Length)
     If (bytesRead = 0) Then Exit While
     fileStrm.Write(buff, 0, bytesRead)

     LngBytesRead += CLng(bytesRead)
     ProgressBar1.Value = CInt((LngBytesRead / respStrm.Length) * 100)
End While

Should work.

I use your code but there's an error says "The stream does not support seek operations" ---> ProgressBar1.Value = CInt((LngBytesRead / respStrm.Length) * 100)

what should i do? Thanks by the way for the reply :)
 
Im probably wrong about this because I dont have much experience with streams but could you do this to get the file length?

VB.NET:
Dim respStrmLength As Long = resp.GetResponseStream().Length

and then change this...

ProgressBar1.Value = CInt((LngBytesRead / respStrm.Length) * 100)

to this...

ProgressBar1.Value = CInt((LngBytesRead / respStrmLength) * 100)

Good Luck. :)
 
Im probably wrong about this because I dont have much experience with streams but could you do this to get the file length?

VB.NET:
Dim respStrmLength As Long = resp.GetResponseStream().Length

and then change this...



to this...



Good Luck. :)

Thanks for your reply, but there's an error says "The stream does not support seek operations" ---> Dim respStrmLength As Long = resp.GetResponseStream().Length

what should i do. thanks again :)
 
where should i post that?
In relation to progress, where you do need to know the size?

Currently you do all your network operations in UI thread, that is not a good idea at all. VB.Net has lots of functionality for multi-threading, so that is something you should study. For downloads/uploads you can do this much simpler using My.Computer.Network object, it can do those operations asynchronously with the option of displaying UI with progress bar and user cancellation, and all you need is one line of code. Internally the Network class uses the asynchronous DownloadFileAsync and UploadFileAsync methods of WebClient class (which has progress events also), so if you don't want that kind of progress UI you can with much greater simplicity use the WebClient class in your own code. If the WebClient class does not suit your needs and you have to go for something like the FtpWebRequest/FtpWebResponse classes you should have a look at the asynchronous methods they provide, or else use other threading options such as Thread class, Delegate.BeginInvoke, Threadpool class, the Task Parallel library, or BackgroundWorker component that is especially useful in winforms applications. Hope this helps with your networking endeavours.
 
Back
Top