• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1042
  • Last Modified:

problem of ftp download file. I can get directory listing, but unable to get file to download

I am having issues with my code.  I can pull a direcrory list.  Now I want to download a file; what is wrong with the code i have below:

Dim ftpreq As FtpWebRequest = FtpWebRequest.Create(Site)

        With ftpreq
            .UsePassive = False
            .Credentials = New NetworkCredential(UserID, Password)
            .UseBinary = True
            .KeepAlive = True
            .Method = WebRequestMethods.Ftp.DownloadFile
            Using response As System.Net.FtpWebResponse = CType(.GetResponse, System.Net.FtpWebResponse)
                Using responseStream As IO.Stream = response.GetResponseStream
                    Using fs As New IO.FileStream("C:\", IO.FileMode.Create)
                        Dim buffer(2047) As Byte
                        Dim read As Integer = 0
                        Do
                            read = responseStream.Read(buffer, 0, buffer.Length)
                            fs.Write(buffer, 0, read)
                        Loop Until read = 0
                        responseStream.Close()
                        fs.Flush()
                        fs.Close()
                    End Using
                    responseStream.Close()
                End Using
                response.Close()
            End Using
        End With

        ftpreq = Nothing
0
mphillip85
Asked:
mphillip85
  • 6
  • 2
  • 2
  • +1
1 Solution
 
appariCommented:
i think you missed  to give filename you want to save.
try

Using fs As New IO.FileStream("C:\filenametosave.txt", IO.FileMode.Create)
0
 
mphillip85Author Commented:
I fixed where C:\ is now C:\filename
The first using is invalid.  Her is the message:

"The requested URI is invalid for this FTP command."  

This is the same for URI I used when I got the directory listing.

What is diferent about the URI that is in the download section?
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
mphillip85Author Commented:
I believe I am not connecting.  Why would I connect on retrieving the ftp file listing
but not be able to connect to get the file.

This is merely an assumption based on my part.

0
 
giltjrCommented:
If you use the command line ftp client, connect, do a dir, then do a "get c:\filename" will it work?

Typically I would say no, because even on Windows servers, very seldom do you have full access to the root directory on a drive.

You may want to try removing the "C:\" and just have "filename".
0
 
mphillip85Author Commented:
not using command lines.  Using code to download.  As stated above, I am using vb .net 2005.  I am successful in retrieving the directory listing, just not the actual downloading of the file.

The first line, in the code where it starts with "Using" is where it has a problem.  

I have 2 different functions.
1 is to get the list and put it into a listbox
2 is to pick from the listbox and loop through to download each file
3 I will fine tune as to which file I will be downloading but wanting a list before I download.

0
 
giltjrCommented:
I understand that you are using a script.  From what I see you are going to end up issuing the command:


    get c:\filename

What I am asking you do to do is to manually try logging on to the ftp server and issuing the command:

    get c:\filename

to see if it works.  I am making the guess that the ftp server is setup so that you can not use "C:\"  That you just need to use "filename" which would default to the current path on the ftp server.  That is, I believe you should be doing:

    get filename
0
 
mphillip85Author Commented:
ok understand
thanks will try
0
 
mphillip85Author Commented:
what i found what that I need to connect to the actual site first.
change to the dierctory containing the files
then do a get command
and that works
so the download is a little different, than just listing.

With the dirlisting I can use the whole site + subdirectory
but with the download needs to be a change to directory after connection, based on the command line test.
0
 
mphillip85Author Commented:
Can you point me to a working sample of ftp download file?

able to get file via command line ftp

now tweaking to work in vb .net 2005

thanks
0
 
appariCommented:
what value do you have in this variable "Site"
suppose you want to download a file sample.txt from ftp://myserver/folder1/folder1/
your site variable value should be "ftp://myserver/folder1/folder1/sample.txt"
if this value is correct it should work.
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.

  • 6
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now