Sheldon Livingston
asked on
Download file using VB.NET
I use the following code to upload a file to an ftp server:
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebR equest.Cre ate("ftp://www.myserver.com/mytextfile.txt"), System.Net.FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredenti al("user", "pw")
clsRequest.Method = System.Net.WebRequestMetho ds.Ftp.UploadFile
' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllByte s("C:\text .txt")
' upload file...
Dim clsStream As System.IO.Stream = clsRequest.GetRequestStrea m()
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
Anything that easy to download an exe file?
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebR
clsRequest.Credentials = New System.Net.NetworkCredenti
clsRequest.Method = System.Net.WebRequestMetho
' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllByte
' upload file...
Dim clsStream As System.IO.Stream = clsRequest.GetRequestStrea
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
Anything that easy to download an exe file?
You can remove lines 5, 6, 7 & 8... they are irrelevant to the code I posted :)
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx
Try this code.
Try this code.
' Get the object used to communicate with the server.
Dim serverUri As Uri = New Uri("ftp://yourftpipaddress/folder")
Dim request As WebClient = New WebClient()
'This example assumes the FTP site uses anonymous logon.
request.Credentials = New NetworkCredential("YourUserID", "Password")
Try
Dim newFileData() As Byte = request.DownloadData(serverUri.ToString())
System.IO.File.WriteAllBytes(@"C:\Temp\yourfilename",newFileData)
Catch e As WebException
MessageBox.Show(e.Message)
End Try
ASKER
The code is crashing at
Dim clsStream As System.IO.MemoryStream = DirectCast(clsRequest.GetR esponse(), System.Net.FtpWebResponse) .GetRespon seStream()
with the error of
A first chance exception of type 'System.Net.WebException' occurred in System.dll
Is this because it is an exe file?
Dim clsStream As System.IO.MemoryStream = DirectCast(clsRequest.GetR
with the error of
A first chance exception of type 'System.Net.WebException' occurred in System.dll
Is this because it is an exe file?
"First chance" exceptions don't cause applications to crash that I'm aware of--this is the runtime handling the exception for you. Are you getting a different exception?
ASKER
Well... the exe isn't downloading...
It's more than likely the implicit cast to MemoryStream that I gave. This should be better:
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://www.myserver.com/mytextfile.txt"), System.Net.FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential("user", "pw")
clsRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\text.txt")
' upload file...
Dim clsStream As System.IO.Stream = DirectCast(clsRequest.GetResponse(), System.Net.FtpWebResponse).GetResponseStream()
Dim outFile As New System.IO.FileStream("C:\your\new\path.txt", IO.FileMode.Create)
Dim str As New System.IO.MemoryStream()
Dim buffer(clsStream.Length) As Byte
str.Read(buffer, 0, buffer.Length)
str.WriteTo(outFile)
ASKER
Closer! Now I get a 0kb file with the correct file name.
Sorry... forgot to close the stream !!! Doh!!!
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://www.myserver.com/mytextfile.txt"), System.Net.FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential("user", "pw")
clsRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\text.txt")
' upload file...
Dim clsStream As System.IO.Stream = DirectCast(clsRequest.GetResponse(), System.Net.FtpWebResponse).GetResponseStream()
Dim outFile As New System.IO.FileStream("C:\your\new\path.txt", IO.FileMode.Create)
Dim str As New System.IO.MemoryStream()
Dim buffer(clsStream.Length) As Byte
str.Read(buffer, 0, buffer.Length)
str.WriteTo(outFile)
' This should flush the buffer to write the data to the file.
outFile.Close()
str.Close()
clsStream.Close()
ASKER
Same thing...
Issue seems to be:
Dim buffer(clsStream.Length) As Byte
Issue seems to be:
Dim buffer(clsStream.Length) As Byte
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I have tested the below code to download a file from ftp server to your local PC.
Try it. Enter the ftp server ip, folder and file name details, user id and password in the code.
Try it. Enter the ftp server ip, folder and file name details, user id and password in the code.
Imports System.Net
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim serverUri As Uri = New Uri("ftp://YourFtpIPAddress/FolderName/Test1.txt")
Dim request As WebClient = New WebClient()
request.Credentials = New NetworkCredential("FtpUserId", "FtpPassword")
request.Proxy = Nothing
Try
Dim newFileData() As Byte = request.DownloadData(serverUri.ToString())
System.IO.File.WriteAllBytes("D:\Temp\Test1.txt", newFileData)
Catch ex As WebException
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
What about this?
Dim wc As New System.Net.WebClient()
wc.Credentials = New System.Net.NetworkCredenti al("user", "pw")
wc.DownloadFile("ftp://www.myserver.com/mytextfile.txt", "C:\text.txt")
Wayne
Dim wc As New System.Net.WebClient()
wc.Credentials = New System.Net.NetworkCredenti
wc.DownloadFile("ftp://www.myserver.com/mytextfile.txt", "C:\text.txt")
Wayne
ASKER
Sweet! Can't believe you got it as far as you did "from the hip". Well done.
Thank you.
Thank you.
NP. Sorry it took so many posts, but glad to help nonetheless :)
Open in new window