That is not a problem...I am kind of a newbie myself :-)
My codebehind script is as follows:
Imports System.Net
Imports System.IO
Imports WinSCP
Public Class InDirectory
Protected Function ProcessFiles() As Integer
Dim Path As String
Path = "C:\Program Files\WinSCP\WinSCP.exe"
Try
' Setup session options
Dim sessionOptions As New SessionOptions
With sessionOptions
.Protocol = Protocol.sFtp
.HostName = "xx.x.xx.xx"
.UserName = "user"
.Password = "password"
.FtpMode = FtpMode.Passive
.SshHostKey = "ssh-rsa xxxx xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
End With
'Set up the session
Using session As Session = New Session
' Connect
session.ExecutablePath = Path
session.Open(sessionOptions)
Dim transferResult As TransferOperationResult
' Upload files
Dim transferOptions As New TransferOptions
If (session.Opened) Then
transferOptions.TransferMode = TransferMode.Binary
transferResult = session.PutFiles(FileUpload1.SelectedItem, "/", True, transferOptions)
' Throw on any error
transferResult.Check()
End If
' Print results
Dim transfer As TransferEventArgs
For Each transfer In transferResult.Transfers
Console.WriteLine("Upload of {0} succeeded", transfer.FileName)
Console.Read()
Next
End Using
Return 0
Catch e As Exception
Console.WriteLine("Error: {0}", e)
Return 1
End Try
End Function
End Class
My main .aspx form is as follows:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="InDirectory.aspx.vb" Inherits="FTPUploadSite.InDirectory" %>
<%@ Import Namespace="FTPUploadSite" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
font-family: Arial;
font-size: xx-large;
}
</style>
</head>
<body style="height: 797px; width: 1621px; background-color: #CCFFFF">
<form id="form1" runat="server">
<div class="style1" style="text-align: center">
FTP In Directory</div>
<p>
</p>
<script runat="server">
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
ProcessFiles()
'ftp.Credentials = New System.Net.NetworkCredential("dwinn", "america86")
'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 sftpFileStream As New FileStream("C:\" + FileUpload1.FileName, FileMode.Open)
'Getting the length of the file.
Dim sftpBuffer(sftpFileStream.Length) As Byte
MsgBox("Carrying On")
'Reading in the file.
sftpFileStream.Read(sftpBuffer, 0, sftpFileStream.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("dwinn@email.co.uk")
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
Protected Sub butDirectories_Click(ByVal sender As Object, ByVal e As EventArgs) Handles butDirectories.Click
'Takes the user to the FTP Directories screen.
Response.Redirect("form1.aspx")
End Sub
Protected Sub butSystemLogOut(ByVal sender As Object, ByVal e As EventArgs) Handles butSysLogOut.Click
'Takes the user to the User Login screen.
Response.Redirect("form2.aspx")
End Sub
Protected Function ProcessFiles() As Integer
Dim Path As String
Path = "C:\Program Files\WinSCP\WinSCP.exe"
Try
' Setup session options
Dim sessionOptions As New SessionOptions
With sessionOptions
.Protocol = Protocol.sFtp
.HostName = "xx.x.xx.xx"
.UserName = "user"
.Password = "password"
.FtpMode = FtpMode.Passive
.SshHostKey = "ssh-rsa xxxx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
End With
'Set up the session
Using session As Session = New Session
' Connect
session.ExecutablePath = Path
session.Open(sessionOptions)
Dim transferResult As TransferOperationResult
' Upload files
Dim transferOptions As New TransferOptions
If (session.Opened) Then
transferOptions.TransferMode = TransferMode.Binary
transferResult = session.PutFiles(FileUpload1.SelectedItem, "/", True, transferOptions)
' Throw on any error
transferResult.Check()
End If
' Print results
Dim transfer As TransferEventArgs
For Each transfer In transferResult.Transfers
Console.WriteLine("Upload of {0} succeeded", transfer.FileName)
Console.Read()
Next
End Using
Return 0
Catch e As Exception
Console.WriteLine("Error: {0}", e)
Return 1
End Try
End Function
</script>
<p>
</p>
<p>
</p>
<p>
</p>
<p style="text-align: center">
<asp:FileUpload ID="FileUpload1" runat="server" />
</p>
<p style="text-align: center">
</p>
<p style="text-align: center">
</p>
<p style="text-align: center">
<asp:Button ID="butUpload" runat="server" Text="Upload File" />
<asp:Button ID="Button4" runat="server"
PostBackUrl="ftp://xx.x.xx.xx/user/In/" style="margin-left: 1px"
Text="FTP List" />
<asp:Button ID="butDirectories" runat="server" Text="Back to FTP Directories"
Width="176px" />
<asp:Button ID="butSysLogOut" runat="server" Text="Exit the system"
Width="176px" />
</p>
<p style="text-align: center">
</p>
<p style="text-align: center">
</p>
<p style="text-align: center">
</p>
<p style="text-align: center">
</p>
<p style="text-align: center">
</p>
<p style="text-align: center">
</p>
</form>
</body>
</html>
Again, many thanks to you for everything :-)
Last edited by dwinn86 on 2012-08-09 10:23; edited 2 times in total