Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

VB
Imports System.IO
Imports System
Imports System.Collections
Imports System.Configuration
Imports System.Configuration.ConfigurationManager
Imports System.Data.OracleClient
Imports Microsoft.Office.Interop
Imports System.Net.FileWebRequest
Imports System.Net

  Public Sub UploadFile()

        '(ByVal filepath As String, ByVal filename As String)

        Dim oSAveAsDir As String = SaveAsDir & strDate & ".xls"
        Dim FtpUser As String = AppSettings.Get("FtpUserId")
        Dim FtpPassword As String = AppSettings.Get("FtpPassword")
        Dim _UploadPath As String = AppSettings.Get("FTPFolderName")

        '/testarea/cms/support/download/stmt/

        Dim clsRequest As System.Net.FtpWebRequest = _
            DirectCast(System.Net.WebRequest.Create("ftp://filetransfer/testarea/cms/support/download/stmt/"), System.Net.FtpWebRequest)
        clsRequest.Credentials = New System.Net.NetworkCredential(FtpUser, FtpPassword)
        clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

        clsRequest.KeepAlive = False
        ' read in file...
        Dim bFile() As Byte = System.IO.File.ReadAllBytes(oSAveAsDir)

        ' upload file...
        Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
        Dim response As FtpWebResponse = CType(clsRequest.GetResponse(), FtpWebResponse)
        clsStream.Write(bFile, 0, bFile.Length)
        clsStream.Close()
        clsStream.Dispose()

    End Sub



I use the above to upload a generated Excel file to the FTP server.
But i'm having error:
The requested URI is invalid for this FTP Command.

I'm using vb 2005.

Thanks in advance.
Posted
Updated 11-Mar-12 21:16pm
v2

System.Net.WebRequest.Create("ftp://filetransfer/testarea/cms/support/download/stmt/")
creates a directory (look at the back slash at the end!). You must create a file instead.
 
Share this answer
 
Comments
Alan Tuscano 12-Mar-12 4:19am    
Hi,

I got this error after removing the last backslash "/"

The error appears between this lines:
''
Dim bFile() As Byte = System.IO.File.ReadAllBytes(oSAveAsDir)
''
Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()

Error:
The Remote Server returned an error: (550) File unavailable(e.g., file not found, no access)

oSaveAsDir is "c:\target\CMS_WISE06032012.xls"

I check the file, its in the destination folder when i save it after generating via excelAppWB.Saveas().

I also exited
excelAppWB = nothing
excelApp.quit after saving.
Bernhard Hiller 12-Mar-12 10:42am    
What about
System.Net.WebRequest.Create("ftp://filetransfer/testarea/cms/support/download/stmt/CMS_WISE06032012.xls")?
Alan Tuscano 12-Mar-12 20:25pm    
Hi Bernhard,

That was also the problem, actually, its d main problem, I can't upload the Excel File unto the FTP Server.

I can access the Ftp Server, I tried dragging a file into it
<pre lang="vb">ftp://filetransfer/testarea/cms/support/download/stmt</pre> and I can. I opened the file i drag at the ftp server and i can view its contents. only when i run my program, generate a excel report and upload it at the FTP server that i can't upload the Excel report.
URI must include a filename at the end: ftp://filetransfer/testarea/cms/support/download/stmt/TEST.XSL
 
Share this answer
 
v2
Comments
CHill60 17-Jun-13 18:22pm    
Essentially the same as solution 1... however, I advise you to take your email address out of your user name or you may be the target of spam

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