|


Add References ... WinSCPnet.DLL ...
โค้ดการใช้งาน FTP ด้วยของฟรีจาก WinSCP .Net (DLL อยู่ใน Source Code) แอดมินเลือกใช้ Hosting ของฟรีจาก www.serv00.com ซึ่งต้องไปสมัครเข้าใช้งานก่อนครับ ... Hosting ตัวนี้เป็น Linux Server ให้พื้นที่มา 3 GB. ซึ่งจะให้ Sub Domain มาด้วย จุดประสงค์ก็เพื่อที่จะทำการอัพโหลดไฟล์ภาพไปเก็บเอาไว้ ก่อนที่จะส่งภาพต่อไปยัง Line Messaging API เพราะ Line ไม่รองรับการอัปโหลดไฟล์ภาพหรือวิดีโอโดยตรงจากเครื่องผู้ใช้ (เช่น C:\image.jpg) ผ่าน API ประเภท push หรือ reply message LINE ต้องการไฟล์ผ่าน URL เท่านั้น ...
มาดูโค้ดต้นฉบับกันเถอะ ...
- Imports System.IO
- Imports System.Net
- Imports WinSCP
- Public Class frmWinSCPFtp
- '// ------------------------------------------------------------------------------------------------
- '// แยกชื่อไฟล์+นามสกุล ออกจากโฟลเดอร์ เพื่อทำการ Upload ไปยัง Hosting
- Dim UploadFileName As String = ""
- '// Default path.
- Dim PicturePath As String = MyPath(Application.StartupPath) & "Images"
- '// ตำแหน่งของไฟล์ภาพบน Hosting เพื่อนำมาแสดงผล (โฟลเดอร์ uploadimages อยู่ภายใต้ public_html)
- '// ต้องเปลี่ยน Sub Domain จาก g2gnet ให้เป็นไปตามที่สมัครเอาไว้ด้วยล่ะครับ
- Dim MyURL As String = "https://g2gnet.serv00.net/uploadimages/"
- '// ตำแหน่งในการอัพโหลดไฟล์ภาพ (FTP)
- Dim RemoteDir As String = "domains/g2gnet.serv00.net/public_html/uploadimages/"
- '// For Login to Hosting.
- Dim HostName As String = "HOST_NAME"
- Dim UName As String = "USER_NAME"
- Dim Pwd As String = "PASSWORD"
- '// ------------------------------------------------------------------------------------------------
- '// START HERE
- Private Sub frmWinSCPFtp_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
- '// Local image file.
- picData.Image = Image.FromFile(PicturePath + "NoImage.gif")
- picURL.Image = Image.FromFile(PicturePath + "NoImage.gif")
- lblRemoteDir.Text = MyURL
- lblLocalDir.Text = "File Upload: "
- End Sub
- Private Sub txtLocalFileName_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtLocalFileName.KeyPress
- '// Lock keypress.
- e.Handled = True
- End Sub
- '// ------------------------------------------------------------------------------------------------
- '// เลือกรูปภาพในการ Upload.
- '// ------------------------------------------------------------------------------------------------
- Private Sub tsBrowse_Click(sender As System.Object, e As System.EventArgs) Handles tsBrowse.Click
- Dim dlgImage As OpenFileDialog = New OpenFileDialog()
- '// Open File Dialog
- With dlgImage
- .InitialDirectory = PicturePath
- .Title = "Select Image File"
- .Filter = "Format (*.jpg;*.jpeg;*.png)|*.jpg;*.jpeg;*.png"
- .FilterIndex = 1
- .RestoreDirectory = True
- End With
- '// Select OK after Browse ...
- If dlgImage.ShowDialog() = DialogResult.OK Then
- '// Check file size don't over 1 MB.
- Dim info As New FileInfo(dlgImage.FileName)
- If (info.Length / 1024) > 1024 Then
- MessageBox.Show("The image file you selected is of size " & Format((info.Length / 1024), "#,##0") & " KB. which is too big more than 1,024 KB.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Warning)
- Exit Sub
- End If
- '// แสดงผลตำแหน่งไฟล์ภาพไว้ใน TextBox Control
- txtLocalFileName.Text = dlgImage.FileName
- '// Put the current image file into PictureBox Control.
- picData.Image = Image.FromFile(dlgImage.FileName)
- '// เอาเฉพาะชื่อไฟล์และนามสกุลภาพ (Filename + Extension) เช่น thongkorn.png
- UploadFileName = dlgImage.SafeFileName
- '// เคลียร์ภาพจากการโหลด Remote
- picURL.Image = Image.FromFile(PicturePath & "NoImage.gif")
- lblLocalDir.Text = "File Upload: " & txtLocalFileName.Text
- End If
- End Sub
- '// ------------------------------------------------------------------------------------------------
- '// UPLOAD FILE.
- '// ------------------------------------------------------------------------------------------------
- Private Sub tsUpload_Click(sender As System.Object, e As System.EventArgs) Handles tsUpload.Click
- If txtLocalFileName.Text.Trim.Length = 0 Then Return
- If picData.Image Is Nothing Or UploadFileName Is Nothing Or UploadFileName.Length = 0 Or UploadFileName = "" Then
- MessageBox.Show("Please select the image file first.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Warning)
- Return
- End If
- '//
- Me.Cursor = Cursors.WaitCursor
- Try
- '// Setup session options
- Dim sessionOptions As New SessionOptions
- With sessionOptions
- .Protocol = Protocol.Ftp
- .HostName = HostName
- .UserName = UName
- .Password = Pwd
- '// FTP ไม่ต้องใช้
- '.SshHostKeyFingerprint = "ssh-rsa 2048 XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX"
- End With
- Using Session As New Session
- '// Connect
- Session.Open(sessionOptions)
- '// Upload files
- Dim TransferOptions As New TransferOptions
- TransferOptions.TransferMode = TransferMode.Binary
- Dim TransferResult As TransferOperationResult
- '// ตำแหน่งไฟล์ต้นฉบับแบบ Full Path และ Remote Directory ปลายทาง
- TransferResult = Session.PutFiles(txtLocalFileName.Text, RemoteDir, False, TransferOptions)
- '// Throw on any error
- TransferResult.Check()
- '// Show results.
- For Each Transfer In TransferResult.Transfers
- MessageBox.Show("Upload " & UploadFileName & " Successful.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
- Next
- End Using
- '// Download image file and show on PictureBox (picURL).
- Dim Req As WebRequest = WebRequest.Create(MyURL & UploadFileName)
- Dim Res As WebResponse = Req.GetResponse()
- Dim imgStream As Stream = Res.GetResponseStream()
- Dim imgPic As Image = Image.FromStream(imgStream)
- imgStream.Close()
- '// Load File Stream into PictureBox.
- With picURL
- picURL.Image = imgPic
- .WaitOnLoad = True
- .SizeMode = PictureBoxSizeMode.StretchImage
- End With
- Catch ex As Exception
- MessageBox.Show(ex.Message)
- Finally
- Me.Cursor = Cursors.Default
- lblLocalDir.Text = "File Upload: " & txtLocalFileName.Text
- End Try
- End Sub
- '// ------------------------------------------------------------------------------------------------
- '// เคลียร์รูปภาพออกจาก PictureBox และ TextBox
- '// ------------------------------------------------------------------------------------------------
- Private Sub tsClear_Click(sender As System.Object, e As System.EventArgs) Handles tsClear.Click
- txtLocalFileName.Clear()
- '// Local image file.
- picData.Image = Image.FromFile(PicturePath & "NoImage.gif")
- picURL.Image = Image.FromFile(PicturePath & "NoImage.gif")
- UploadFileName = ""
- lblLocalDir.Text = "File Upload: "
- End Sub
- Private Sub tsExit_Click(sender As System.Object, e As System.EventArgs) Handles tsExit.Click
- Me.Close()
- End Sub
- Private Sub frmWinSCPFtp_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
- Me.Dispose()
- GC.SuppressFinalize(Me)
- Application.Exit()
- End Sub
- #Region "FUNCTION"
- '// --------------------------------------------------------------------------------
- '// Get my project path
- '// AppPath = C:\My Project\bin\debug
- '// Replace "\bin\debug" with ""
- '// Return : C:\My Project\
- Function MyPath(ByVal AppPath As String) As String
- '// Return Value
- MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "").Replace("\bin\x86\release", "")
- '// If not found folder then put the \ (BackSlash) at the end.
- If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
- End Function
- #End Region
- End Class
คัดลอกไปที่คลิปบอร์ด
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) + .Net Framework 4.0 ได้ที่นี่ ...
|
ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง
คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน
x
|