[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

vb.net FTP Download / File Size

Posted on 2007-12-06
3
Medium Priority
?
15,682 Views
Last Modified: 2013-12-16
I have aprogram which connects to the FTP server I have the upload peice working I can get he file directory yada yada where the problem lies is
1 - Cant get it to download the code completely(ill attach my download code and the error)
2 - I need to figure out how toget the file size befor I download so I can implement my progress bar and you will see where I have tried "response.contentlength" but it returns "-1"
------------
Problem for #1 is listed underneath the code
-------------------

Thanks for any help!
Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
        fdb.SelectedPath = Application.StartupPath
        fdb.ShowDialog()
        Dim tempSaveDir = fdb.SelectedPath
        Try
            Dim outputStream As FileStream = New FileStream(fdb.SelectedPath + "\\" + lbFiles.SelectedItem, FileMode.OpenOrCreate)
            Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(tempurl & "/" & lbFiles.SelectedItem), System.Net.FtpWebRequest)
            clsRequest.Credentials = New System.Net.NetworkCredential(Form1.tbUser.Text, Form1.tbPass.Text)
            clsRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
 
            Dim response As FtpWebResponse = clsRequest.GetResponse
            Dim ftpStream As Stream = response.GetResponseStream()
            Dim cl As String = response.ContentLength
            MsgBox(cl)
            Dim bufferSize As Integer = 1024
            Dim readCount As Integer
            Dim buffer(bufferSize) As Byte
            Dim tempCount = 0
            ProgressBar.Value = 0
            While True
                readCount = ftpStream.Read(buffer, 0, bufferSize)
                If readCount > 0 Then
                    outputStream.Write(buffer, 0, readCount)
                    'ProgressBar.Value = (tempCount / cl) * 100
                End If
                'MsgBox(outputstre)
            End While
 
            ftpStream.Flush()
            outputStream.Flush()
            ftpStream.Dispose()
            outputStream.Dispose()
            response.Close()
 
        Catch ui As UriFormatException
            MsgBox("ui " + ui.Message)
        Catch io As IOException
            MsgBox("IO " + io.Message)
        Catch ex As Exception
            MsgBox("EX " + ex.Message)
        End Try
    End Sub
 
 
ERROR I receive when trying to download I get about 75% of the way thru the download - 
EX Cannot access adisposed object.
Object name: 'system.net.sockets.networkstream'.
 
Then it just stops and aborts the download all this doesnt make sense because im not disposing any object untill after the read AND I commented the code out and still doenst work.

Open in new window

0
Comment
Question by:aumudin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 19

Accepted Solution

by:
Rimvis earned 1200 total points
ID: 20426234
You have an endless loop. Maybe this is the reason? It never stops, even after download is finished

Take a look at this code:

...
            clsRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
 
            Dim cl As String
            Dim response As FtpWebResponse = clsRequest.GetResponse
 
            If response.ContentLength > 0 Then
                cl = response.ContentLength
            Else
                'Get filesize from status message
                Dim r As Regex = New Regex("(?<size>\d+[ ]bytes)")
                Dim m As Match = r.Matches(response.StatusDescription)("size")
                cl = m.Value
            End If
 
            Dim ftpStream As Stream = response.GetResponseStream()
            Dim bufferSize As Integer = 1024
            Dim readCount As Integer
            Dim buffer(bufferSize) As Byte
            Dim tempCount = 0
            ProgressBar.Value = 0
            While True
                readCount = ftpStream.Read(buffer, 0, bufferSize)                
                If readCount > 0 Then
                    outputStream.Write(buffer, 0, readCount)
                    tempCount += readCount
                    ProgressBar.Value = (tempCount / cl) * 100
                    ProgressBar.Refresh()
                Else
                    'Exit loop
                    Exit While
                End If
                'MsgBox(outputstre)
            End While
...

Open in new window

0
 
LVL 16

Assisted Solution

by:gnoon
gnoon earned 800 total points
ID: 20426266
This's from MSDN

"
ContentLength returns 1 if no data was returned in the response or if the server did not send content length information. The return value is greater than or equal to zero if data was or should have been returned
"

Print the URL to the console, then copy/paste at a browser. Hits Enter and see if any file is downloaded.

Console.WriteLine(tempurl & "/" & lbFiles.SelectedItem)

Output?

P.S. I noticed that you're facing infinite loop of reading (no such line of break), and I think it's the cause of error. Try change it to this
While True
	readCount = ftpStream.Read(buffer, 0, bufferSize)
	If readCount > 0 Then
		outputStream.Write(buffer, 0, readCount)
		'ProgressBar.Value = (tempCount / cl) * 100
	Else
		Exit While
	End If
End While

Open in new window

0
 
LVL 4

Author Comment

by:aumudin
ID: 20427746
Alright and as far as getting the file size I found the answer its ftpwebresponse.getFileSize pretty simple lol as far as the infinite loop I cant belive I didnt see that...... Thanks guys
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question