Question : vb.net ftp doesnt work right

i'm trying to download an image file down from a web server. i am successfully getting it down to a local machine but i can't view the file. here is my code. please help me thanks.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
Dim myFtpWebRequest As Net.FtpWebRequest
        Dim myFtpWebResponse As Net.FtpWebResponse
        Dim myStreamWriter As IO.StreamWriter

        myFtpWebRequest = WebRequest.Create("ftp://website address" & file)
        myFtpWebRequest.Credentials = New Net.NetworkCredential("username", "password")
        myFtpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile
        ' myFtpWebRequest.UseBinary = True
        myFtpWebResponse = myFtpWebRequest.GetResponse()
        myStreamWriter = New IO.StreamWriter("c:\photocakes\" & file)
        myStreamWriter.Write(New IO.StreamReader(myFtpWebResponse.GetResponseStream()).ReadToEnd)
        myStreamWriter.Close()
        myFtpWebResponse.Close()

Answer : vb.net ftp doesnt work right

Have a look at the example below. Remember that most request from ftp sites can be case sensitive so make sure the location you point to on the server matches exactly.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
Imports System.Net
Imports System.IO
Imports System.Text

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim fs As FileStream = Nothing
        Dim fwr As FtpWebRequest = Nothing
        Dim ftpUrl As New StringBuilder
        Dim data(4096) As Byte ' default ftp buffer size
        Dim bytesRead As Integer = 0
        ftpUrl.Append("ftp://ftp.server.com:21/") 'ftp url + port
        Dim ftpUser As String = "username"
        Dim ftpPass As String = "password"
        Dim ftpFile As String = "TEST/file.jpg" 'folder/file on server
        Dim savePath As String = "c:\users\username\documents\file.jpg" ' save location
        ftpUrl.Append(ftpFile)
        Try
            fwr = FtpWebRequest.Create(ftpUrl.ToString)
            fwr.UseBinary = True
            fwr.Credentials = New NetworkCredential(ftpUser, ftpPass)
            fwr.Method = WebRequestMethods.Ftp.DownloadFile
            fs = New FileStream(savePath, FileMode.Create, FileAccess.Write, FileShare.Read)
            Do
                bytesRead = fwr.GetResponse.GetResponseStream.Read(data, 0, data.Length)
                fs.Write(data, 0, bytesRead)
            Loop Until bytesRead = 0
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If fs IsNot Nothing Then
                fs.Close()
                fs = Nothing
            End If
        End Try
    End Sub
End Class
Random Solutions  
 
programming4us programming4us