Question I can get directory listing, but unable to get file to download :(

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)
        URL = "ftp://" & txturl.Text
        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 = "Auflistung komplett!"
        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)
        URL = "ftp://" & txturl.Text


     Dim requ As FtpWebRequest = Nothing
        Dim resp As FtpWebResponse = Nothing
        Dim respStrm As Stream = Nothing
        Dim fileStrm As FileStream = Nothing
        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 komplett!"
            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
End Class


On ToolStripStatusLabel2.Text it appears the error when i click the download button
The error is this: [the requested uri is invalid for this ftp command]



Please help me. I'm running out of time :((
Thanks in advance ! i'm working for my thesis :(
 
Last edited by a moderator:
In downloadFTP method for Create(URL) call have a look at the value of URL variable, that is what is wrong.
 
Back
Top