make sure you have this code at the top of all codes
Code:Imports System.Net
Printable View
make sure you have this code at the top of all codes
Code:Imports System.Net
Sorry for the bump, but I really need this.
Your code looks good to me, but I'm just as reconray getting an error at:
VB Code:
Dim clsStream As Stream = clsRequest.GetRequestStream()
The error is: Cannot send a content-body with this verb-type.
I've got 2 imports at the top of my file:
VB Code:
Imports System.Net Imports System.IO
Thanks.
Hi,
i am using this code but getting error. please help
Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
If txtFileName.Text <> String.Empty Then
Dim filename As String = Me.txtFileName.Text
Dim fileInf As New FileInfo(filename)
'Upload(filename)
Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(txtUploadSvr.Text & fileInf.Name), System.Net.FtpWebRequest)
clsRequest.Credentials = New System.Net.NetworkCredential(txtUser.Text, txtPassword.Text)
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllBytes(txtFileName.Text)
' upload file...
Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
End If
End Sub
Hello,
I'm a semi-newbie and having a problem compiling the following code that I wanted to combine with the above to upload a file to an FTP server:
For some reason, VB.Net (2008 Express) complains with "Constant expression is required" when using "server" and "FullFileName".Code:Dim server As String
server = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\My very own key", "server", Nothing)
'Constant expression is required
Const FtpFullFileName As String = "ftp://" & server & "/" & FileName
Const FullFileName As String = "C:\Temp\test.txt"
'Constant expression is required
Const FileName As String = IO.Path.GetFileName(FullFileName)
I get the same error if using Const instead of Dim:
Can someone spot the error?Code:Const server As String = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\My very own key", "server", Nothing)
Thank you.
try this myb it will solve the problem
Code:Dim server As new String
My.Computer.Registry.GetValue("HKEY_CURRENT_USER\My very own key", "server", Nothing).tostring
Thanks for the tip.
Hi all,
for those who are interested, here is some sample (working) VB 2005 code to upload multiple files via FTP. the code includes the following extras. will check if internet connection is active, upload each file, and then move it to an archive directory. Hope this helps
Imports System
Imports System.IO
Imports System.Net
Imports System.Runtime.InteropServices
Imports System.Text
Imports OpenNETCF.Net
Imports OpenNETCF.Net.Ftp
Public Class Form1
Public Function IsConnectionAvailable() As Boolean
' Returns True if connection is available
' Replace www.yoursite.com with a site that
' is guaranteed to be online - perhaps your
' corporate site, or microsoft.com
Dim objUrl As New System.Uri("http://(FTP server address)") ' without ()
' Setup WebRequest
Dim objWebReq As System.Net.WebRequest
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim objResp As System.Net.WebResponse
Try
' Attempt to get response and return True
objResp = objWebReq.GetResponse
objResp.Close()
objWebReq = Nothing
Return True
Catch ex As Exception
' Error, exit and return False
'objResp.Close()
objWebReq = Nothing
Return False
End Try
End Function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If IsConnectionAvailable() = True Then
Label4.Text = "Internet connection Active"
Label6.Text = "Uploading Files"
'' FTP Server
Dim sFTPServer As String = "ftp://(FTP server address)" ' without ()
'' FTP User Account
Dim sUserName As String = "(username)" ' without ()
''FTP sPassword
Dim sPassword As String = "(password)" ' without ()
For Each sFilePath As String In IO.Directory.GetFiles("c:\folder\")
Try
Dim FtpRequestCreator As New FtpRequestCreator
WebRequest.RegisterPrefix("ftp:", FtpRequestCreator)
Dim oFtpWebRequest As New FtpWebRequest(New Uri(sFTPServer))
oFtpWebRequest.Credentials = New NetworkCredential(sUserName, sPassword)
' Getting the Request stream
Dim oFtpRequestStream As Stream = oFtpWebRequest.GetRequestStream()
Dim oStreamReader As New StreamReader(oFtpRequestStream)
'Just ignore the result, but read it.
Dim oReader As String = oStreamReader.ReadToEnd
'Open the input file. If the file does not exist, it's an error.
Dim oFileStream As New FileStream(sFilePath, FileMode.Open)
'Create the reader for the local file data.
Dim oFileReader As BinaryReader = New BinaryReader(oFileStream)
'Opening the data connection, this must be done before we issue the command.
Dim oFtpResponseStream As Stream = oFtpWebRequest.GetResponse().GetResponseStream()
Dim oDataWriter As New BinaryWriter(oFtpResponseStream)
'Prepare to send commands to the server.
Dim cmdWriter As New StreamWriter(oFtpRequestStream)
'Set transfer type to IMAGE (binary).
cmdWriter.Write("TYPE I\r\n" & vbCrLf)
cmdWriter.Flush()
'Reading the request output
Dim oStreamReader2 As New StreamReader(oFtpRequestStream)
oReader = oStreamReader.ReadToEnd()
'Reading the request output
Dim sReader As String
sReader = oStreamReader2.ReadToEnd()
'Write the command to the request stream.
cmdWriter.Write("STOR " + Path.GetFileName(sFilePath) + vbCrLf)
cmdWriter.Flush()
sReader = oStreamReader2.ReadToEnd()
'Allocate buffer for the data, which will be written in blocks.
Dim bytes As Integer
Dim bufsize As Integer = 4096
Dim buffer(bufsize) As Byte
While True
bytes = oFileReader.Read(buffer, 0, bufsize)
oDataWriter.Write(buffer, 0, bytes)
If bytes <= 0 Then
Exit While
End If
End While
oFileReader.Close()
oFileStream.Close()
oDataWriter.Close()
oStreamReader.Close()
Dim foundFileInfo As New System.IO.FileInfo(sFilePath)
IO.File.Move(sFilePath, "C:\Folder\archive\" & foundFileInfo.Name)
'Label6.Text = "Uploading Complete"
Catch ex As Exception
sFilePath = Nothing
End Try
System.Threading.Thread.Sleep(5000)
Next
Else
Label4.Text = "No internet connection"
Label6.Text = "No FTP in progress"
End If
End Sub
End Class