Hi everyone,
I have developed a site using VB.Net and Visual Studio 2010, that allows users to upload and download multiple files to an FTP server.
I have recently been given a request to change this so that SFTP is used instead of FTP.
I have downloaded WinSCP but am now stumped as to what I need to do next.
Can somebody please advise me on what I need to put in to my code so that my site works with WinSCP to upload and download filed in SFTP rather than FTP.
Here is my code at the moment to upload files:
Protected Sub butUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles butUpload.Click
'Declaring the path of where the file should be uploaded to on the FTP site.
Dim ftp As FtpWebRequest = DirectCast(WebRequest.Create("ftp://XX.X.XX.XX/In/" + FileUpload1.FileName), FtpWebRequest)
Dim objOutlook As Object
Dim objOutlookMsg As Object
'If no file has been selected, give error message.
If FileUpload1.HasFile = False Then
MsgBox("A file needs to be selected!")
End If
'If a file has been selected, carry out the file upload process.
If FileUpload1.HasFile = True Then
Try
ftp.Credentials = New System.Net.NetworkCredential("user", "password")
ftp.KeepAlive = False
ftp.Timeout = 20000
ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile
ftp.UseBinary = True
Const olMailItem = 0
'The importance level of the email.
Const olImportanceHigh = 1
'Who the email is going to.
Const olTo = 1
objOutlook = CreateObject("Outlook.Application")
'Creating new mail item.
objOutlookMsg = objOutlook.CreateItem(olMailItem)
'Declaring the directory to read the file from.
Dim ftpFileStream As New FileStream("C:\" + FileUpload1.FileName, FileMode.Open)
'Getting the length of the file.
Dim ftpBuffer(ftpFileStream.Length) As Byte
MsgBox("Carrying On")
'Reading in the file.
ftpFileStream.Read(ftpBuffer, 0, ftpFileStream.Length)
'Upload the file
Dim ftpGetStream As Stream = ftp.GetRequestStream()
ftpGetStream.Write(ftpBuffer, 0, ftpFileStream.Length)
'Closes off connections.
ftpGetStream.Close()
ftpFileStream.Close()
'Getting a response from the FTP site.
Dim ftpResponse As FtpWebResponse = CType(ftp.GetResponse(), FtpWebResponse)
'SCRIPT TO SEND EMAIL GOES HERE!
With objOutlookMsg
'The email address of the recipient.
Dim objOutlookRecip As Object = .Recipients.Add("email")
objOutlookRecip.type = olTo
'Declaring the subject of the email.
.Subject = "FTP File Upload Confirmation"
'Declaring what will be in the email.
.Body = "Hello, this is confirmation that the file: " + FileUpload1.FileName + " has been uploaded successfully..."
'The importance level of the email.
.Importance = olImportanceHigh
'Sending the email.
.send()
End With
objOutlookMsg = Nothing
objOutlook = Nothing
Catch ex As Exception
MsgBox("File: " + FileUpload1.FileName + " has been uploaded...")
End Try
End If
End Sub
All help and advice will be greatly appreciated.
Dan