Click here to Skip to main content
15,887,304 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I try to use a SQL Table to find a file on an FTP server that I can access via regular HTTP.

I use the login user name to retrieve the correct name of the file.

My problem is that I do not now how to access the ftp.
I have tried different methods and guides, but everyone explains how to download or upload the file to the user, and not how I can acces it to use the content inside my code behind. (VB script)

the sql receive part works.


VB
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        
        Dim myuser As MembershipUser
        myuser = Membership.GetUser(User.Identity.Name)
        Dim myuserString As String
        myuserString = myuser.UserName
                      
        Dim strQuery As String = ("SELECT UserID, kFile FROM [Userinfo] WHERE [UserID] = @UserID")
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("UserinfoConnectionString").ConnectionString)
        Dim cmd As New SqlCommand()
        cmd.Parameters.AddWithValue("@UserID", myuser.UserName)
        cmd.CommandType = CommandType.Text
        cmd.CommandText = strQuery
        cmd.Connection = con
        
        Try
            con.Open()
            Dim sdr As SqlDataReader = cmd.ExecuteReader()
            While sdr.Read()

                UserIDLabel.Text = sdr("UserID").ToString()
                KioskFileLabel.Text = sdr("kFile").ToString()

                Const server As String = "http://192.168.3.50:81/_0/"
                Dim address As String = server + sdr("kFile")
                Dim file As New StreamReader(address, System.Text.Encoding.UTF8)
                
                FTPFileLabel.Text = file.ReadToEnd()
                
                file.Close()
                
            End While

        Finally

            con.Close()
            con.Dispose()
            
        End Try
    End Sub
Posted
Updated 18-Feb-14 8:50am
v2
Comments
prototypen 18-Feb-14 15:07pm    
thank you

0) FTP or HTTP than?
1) StreamReader does not use any protocol handler. You can't use it to read a resource on an URL/URI.
2) Use WebRequest[^] class instead. It is quite sophisticated class, you can use it for both ftp and http requests.
+1) You still download. There is no actual difference in terms of http or ftp traffic if you save the retrieved content to a file, or not.
 
Share this answer
 
v2
Comments
prototypen 18-Feb-14 15:25pm    
thanks now it works
From code
VB
Const server As String = "http://192.168.3.50:81/_0/"

I changed the code to

VB
Dim request As WebRequest = WebRequest.Create("http://server/" + sdr("kFile"))
               Dim response As WebResponse = request.GetResponse()

               Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)

               Dim dataStream As Stream = response.GetResponseStream()

               Dim reader As New StreamReader(dataStream)

               Dim responseFromServer As String = reader.ReadToEnd()

               Console.WriteLine(responseFromServer)



               FTPFileLabel.Text = responseFromServer

               reader.Close()
               response.Close()
           End While

       Finally

           con.Close()
           con.Dispose()


       End Try
   End Sub


and thanks to Zoltán Zörgő for the help.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900