ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 88|ตอบกลับ: 0

[VB.NET] โค้ดการใช้งาน FTP (File Transfer Protocol) ด้วยของฟรีจาก WinSCP.Net

[คัดลอกลิงก์]

326

กระทู้

519

โพสต์

7025

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
7025




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 เท่านั้น ...

มาดูโค้ดต้นฉบับกันเถอะ ...
  1. Imports System.IO
  2. Imports System.Net
  3. Imports WinSCP

  4. Public Class frmWinSCPFtp
  5.     '// ------------------------------------------------------------------------------------------------
  6.     '// แยกชื่อไฟล์+นามสกุล ออกจากโฟลเดอร์ เพื่อทำการ Upload ไปยัง Hosting
  7.     Dim UploadFileName As String = ""
  8.     '// Default path.
  9.     Dim PicturePath As String = MyPath(Application.StartupPath) & "Images"

  10.     '// ตำแหน่งของไฟล์ภาพบน Hosting เพื่อนำมาแสดงผล (โฟลเดอร์ uploadimages อยู่ภายใต้ public_html)
  11.     '// ต้องเปลี่ยน Sub Domain จาก g2gnet ให้เป็นไปตามที่สมัครเอาไว้ด้วยล่ะครับ
  12.     Dim MyURL As String = "https://g2gnet.serv00.net/uploadimages/"
  13.     '// ตำแหน่งในการอัพโหลดไฟล์ภาพ (FTP)
  14.     Dim RemoteDir As String = "domains/g2gnet.serv00.net/public_html/uploadimages/"
  15.     '// For Login to Hosting.
  16.     Dim HostName As String = "HOST_NAME"
  17.     Dim UName As String = "USER_NAME"
  18.     Dim Pwd As String = "PASSWORD"

  19.     '// ------------------------------------------------------------------------------------------------
  20.     '// START HERE
  21.     Private Sub frmWinSCPFtp_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  22.         '// Local image file.
  23.         picData.Image = Image.FromFile(PicturePath + "NoImage.gif")
  24.         picURL.Image = Image.FromFile(PicturePath + "NoImage.gif")
  25.         lblRemoteDir.Text = MyURL
  26.         lblLocalDir.Text = "File Upload: "
  27.     End Sub

  28.     Private Sub txtLocalFileName_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtLocalFileName.KeyPress
  29.         '// Lock keypress.
  30.         e.Handled = True
  31.     End Sub

  32.     '// ------------------------------------------------------------------------------------------------
  33.     '// เลือกรูปภาพในการ Upload.
  34.     '// ------------------------------------------------------------------------------------------------
  35.     Private Sub tsBrowse_Click(sender As System.Object, e As System.EventArgs) Handles tsBrowse.Click
  36.         Dim dlgImage As OpenFileDialog = New OpenFileDialog()
  37.         '// Open File Dialog
  38.         With dlgImage
  39.             .InitialDirectory = PicturePath
  40.             .Title = "Select Image File"
  41.             .Filter = "Format (*.jpg;*.jpeg;*.png)|*.jpg;*.jpeg;*.png"
  42.             .FilterIndex = 1
  43.             .RestoreDirectory = True
  44.         End With
  45.         '// Select OK after Browse ...
  46.         If dlgImage.ShowDialog() = DialogResult.OK Then
  47.             '// Check file size don't over 1 MB.
  48.             Dim info As New FileInfo(dlgImage.FileName)
  49.             If (info.Length / 1024) > 1024 Then
  50.                 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)
  51.                 Exit Sub
  52.             End If
  53.             '// แสดงผลตำแหน่งไฟล์ภาพไว้ใน TextBox Control
  54.             txtLocalFileName.Text = dlgImage.FileName
  55.             '// Put the current image file into PictureBox Control.
  56.             picData.Image = Image.FromFile(dlgImage.FileName)
  57.             '// เอาเฉพาะชื่อไฟล์และนามสกุลภาพ (Filename + Extension) เช่น thongkorn.png
  58.             UploadFileName = dlgImage.SafeFileName
  59.             '// เคลียร์ภาพจากการโหลด Remote
  60.             picURL.Image = Image.FromFile(PicturePath & "NoImage.gif")
  61.             lblLocalDir.Text = "File Upload: " & txtLocalFileName.Text
  62.         End If
  63.     End Sub

  64.     '// ------------------------------------------------------------------------------------------------
  65.     '// UPLOAD FILE.
  66.     '// ------------------------------------------------------------------------------------------------
  67.     Private Sub tsUpload_Click(sender As System.Object, e As System.EventArgs) Handles tsUpload.Click
  68.         If txtLocalFileName.Text.Trim.Length = 0 Then Return
  69.         If picData.Image Is Nothing Or UploadFileName Is Nothing Or UploadFileName.Length = 0 Or UploadFileName = "" Then
  70.             MessageBox.Show("Please select the image file first.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  71.             Return
  72.         End If
  73.         '//
  74.         Me.Cursor = Cursors.WaitCursor
  75.         Try
  76.             '// Setup session options
  77.             Dim sessionOptions As New SessionOptions
  78.             With sessionOptions
  79.                 .Protocol = Protocol.Ftp
  80.                 .HostName = HostName
  81.                 .UserName = UName
  82.                 .Password = Pwd
  83.                 '// FTP ไม่ต้องใช้
  84.                 '.SshHostKeyFingerprint = "ssh-rsa 2048 XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX"
  85.             End With
  86.             Using Session As New Session
  87.                 '// Connect
  88.                 Session.Open(sessionOptions)
  89.                 '// Upload files
  90.                 Dim TransferOptions As New TransferOptions
  91.                 TransferOptions.TransferMode = TransferMode.Binary

  92.                 Dim TransferResult As TransferOperationResult
  93.                 '// ตำแหน่งไฟล์ต้นฉบับแบบ Full Path และ Remote Directory ปลายทาง
  94.                 TransferResult = Session.PutFiles(txtLocalFileName.Text, RemoteDir, False, TransferOptions)
  95.                 '// Throw on any error
  96.                 TransferResult.Check()

  97.                 '// Show results.
  98.                 For Each Transfer In TransferResult.Transfers
  99.                     MessageBox.Show("Upload " & UploadFileName & " Successful.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  100.                 Next
  101.             End Using

  102.             '// Download image file and show on PictureBox (picURL).
  103.             Dim Req As WebRequest = WebRequest.Create(MyURL & UploadFileName)
  104.             Dim Res As WebResponse = Req.GetResponse()
  105.             Dim imgStream As Stream = Res.GetResponseStream()
  106.             Dim imgPic As Image = Image.FromStream(imgStream)
  107.             imgStream.Close()
  108.             '// Load File Stream into PictureBox.
  109.             With picURL
  110.                 picURL.Image = imgPic
  111.                 .WaitOnLoad = True
  112.                 .SizeMode = PictureBoxSizeMode.StretchImage
  113.             End With
  114.         Catch ex As Exception
  115.             MessageBox.Show(ex.Message)
  116.         Finally
  117.             Me.Cursor = Cursors.Default
  118.             lblLocalDir.Text = "File Upload: " & txtLocalFileName.Text
  119.         End Try
  120.     End Sub

  121.     '// ------------------------------------------------------------------------------------------------
  122.     '// เคลียร์รูปภาพออกจาก PictureBox และ TextBox
  123.     '// ------------------------------------------------------------------------------------------------
  124.     Private Sub tsClear_Click(sender As System.Object, e As System.EventArgs) Handles tsClear.Click
  125.         txtLocalFileName.Clear()
  126.         '// Local image file.
  127.         picData.Image = Image.FromFile(PicturePath & "NoImage.gif")
  128.         picURL.Image = Image.FromFile(PicturePath & "NoImage.gif")
  129.         UploadFileName = ""
  130.         lblLocalDir.Text = "File Upload: "
  131.     End Sub

  132.     Private Sub tsExit_Click(sender As System.Object, e As System.EventArgs) Handles tsExit.Click
  133.         Me.Close()
  134.     End Sub

  135.     Private Sub frmWinSCPFtp_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  136.         Me.Dispose()
  137.         GC.SuppressFinalize(Me)
  138.         Application.Exit()
  139.     End Sub

  140. #Region "FUNCTION"
  141.     '// --------------------------------------------------------------------------------
  142.     '// Get my project path
  143.     '// AppPath = C:\My Project\bin\debug
  144.     '// Replace "\bin\debug" with ""
  145.     '// Return : C:\My Project\
  146.     Function MyPath(ByVal AppPath As String) As String
  147.         '// Return Value
  148.         MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "").Replace("\bin\x86\release", "")
  149.         '// If not found folder then put the \ (BackSlash) at the end.
  150.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  151.     End Function
  152. #End Region
  153. End Class
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) + .Net Framework 4.0 ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2025-5-12 09:23 , Processed in 0.118940 second(s), 5 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้