Wednesday, 15 August 2012

VB.NET Error The remote server returned an error: (553) File name not allowed.

The following code will produce the following error
The remote server returned an error: (553) File name not allowed.

Dim localFolderAndFile As String = "C:\Temp\test.pdf"
Dim remoteFolderAndFile As String = "/Test/"
Dim host As String = "ftp://hostName.com"
Dim username As String = "UserName"
 Dim password As String = "Pass"
Dim fwrRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(host & remoteFolderAndFile), System.Net.FtpWebRequest)
fwrRequest.Credentials = New System.Net.NetworkCredential(username, password) fwrRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Dim bFile() As Byte = System.IO.File.ReadAllBytes(localFolderAndFile)
Dim ioStream As System.IO.Stream = fwrRequest.GetRequestStream()
ioStream.Write(bFile, 0, bFile.Length)
ioStream.Close() ioStream.Dispose()

In order to fix it, replace code above with the code below:

Dim localFolderAndFile As String = "C:\Temp\test.pdf"
Dim remoteFolderAndFile As String = "/Test/test.pdf"
Dim host As String = "ftp://hostName.com"
Dim username As String = "UserName"
Dim password As String = "Pass"
Dim fwrRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(host & remoteFolderAndFile), System.Net.FtpWebRequest)
fwrRequest.Credentials = New System.Net.NetworkCredential(username, password) fwrRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile Dim bFile() As Byte = System.IO.File.ReadAllBytes(localFolderAndFile)
Dim ioStream As System.IO.Stream = fwrRequest.GetRequestStream()
ioStream.Write(bFile, 0, bFile.Length)
ioStream.Close()
ioStream.Dispose()

I had to change
Dim remoteFolderAndFile As String = "/Test/"
to
Dim remoteFolderAndFile As String = "/Test/test.pdf"

No comments:

Post a Comment