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

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

[VB.NET] การอัพโหลดและดาวน์โหลดไฟล์ภาพด้วย WinSCP กับ Free Hosting - Serv00.com

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

311

กระทู้

502

โพสต์

6060

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6060

การอัพโหลดและดาวน์โหลดไฟล์ภาพด้วย WinSCP กับ Free Hosting - wwwServ00.com  






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

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

  10.     ' / --------------------------------------------------------------------------------
  11.     '// สำหรับ HOSTING
  12.     '// ตำแหน่งของไฟล์ภาพบน Hosting เพื่อนำมาแสดงผล
  13.     '// แก้ไข SUB DOMAIN และชื่อ UPLOADDIRECTORY ที่ต้องการอัพโหลดด้วยล่ะกัน
  14.     Dim MyURL As String = "https://SUBDOMAIN.serv00.net/UPLOADDIRECTORY/"
  15.     '// ตำแหน่งในการรีโมทเพื่อที่จะอัพโหลดไฟล์ภาพ (FTP) ...
  16.     Dim RemoteDir As String = "domains/SUBDOMAIN.serv00.net/public_html/UPLOADDIRECTORY/"
  17.     '// Login เข้าสู่ระบบ Hosting ...
  18.     '// FTP Hostname ต้องดูจากตอนที่ Add Account for FTP เพราะอาจจะไม่เหมือนกับตัวอย่าง
  19.     Dim HostName As String = "s0.serv00.com"
  20.     Dim UName As String = "USERNAME"
  21.     Dim Pwd As String = "PASSWORD"

  22.     ' / --------------------------------------------------------------------------------
  23.     ' / S T A R T ... H E R E
  24.     ' / --------------------------------------------------------------------------------
  25.     Private Sub frmFtpServ00WinSCP_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  26.         txtLocalFileName.Text = ""
  27.         '/ Local image file.
  28.         picData.Image = Image.FromFile(PicturePath + "NoImage.gif")
  29.         picURL.Image = Image.FromFile(PicturePath + "NoImage.gif")
  30.         Label1.Text = "File Upload: "
  31.     End Sub

  32.     ' / --------------------------------------------------------------------------------
  33.     ' / เลือกรูปภาพในการ Upload.
  34.     ' / --------------------------------------------------------------------------------
  35.     Private Sub btnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
  36.         Dim dlgImage As OpenFileDialog = New OpenFileDialog()
  37.         ' / Open File Dialog
  38.         With dlgImage
  39.             '.InitialDirectory = PicturePath
  40.             .Title = "เลือกภาพ"
  41.             .Filter = "รูปแบบภาพ (*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp"
  42.             .FilterIndex = 1
  43.             .RestoreDirectory = True
  44.         End With
  45.         ' Select OK after Browse ...
  46.         If dlgImage.ShowDialog() = DialogResult.OK Then
  47.             ' Get file size
  48.             Dim info As New FileInfo(dlgImage.FileName)
  49.             If (info.Length / 1024) > 1024 Then
  50.                 MessageBox.Show("ไฟล์ภาพที่คุณเลือกมีขนาด " & Format((info.Length / 1024), "#,##0") & " KB. ซึ่งมีขนาดใหญ่เกินกว่า 1,024 KB.", "รายงานสถานะ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  51.                 Exit Sub
  52.             End If
  53.             '//
  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.             Label1.Text = "File Upload: " & txtLocalFileName.Text
  60.         End If
  61.     End Sub

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

  91.                 Dim TransferResult As TransferOperationResult
  92.                 TransferResult = Session.PutFiles(txtLocalFileName.Text, RemoteDir, False, TransferOptions)
  93.                 '/ Throw on any error
  94.                 TransferResult.Check()

  95.                 '/ Show results.
  96.                 For Each Transfer In TransferResult.Transfers
  97.                     MessageBox.Show("Upload " & Transfer.FileName & " Successful.")
  98.                 Next
  99.             End Using

  100.             '// Download image file and show on PictureBox (picURL).
  101.             Dim Req As WebRequest = WebRequest.Create(MyURL & UploadFileName)
  102.             Dim Res As WebResponse = Req.GetResponse()
  103.             Dim imgStream As Stream = Res.GetResponseStream()
  104.             Dim imgPic As Image = Image.FromStream(imgStream)
  105.             imgStream.Close()
  106.             '/ Load File Stream into PictureBox.
  107.             With picURL
  108.                 picURL.Image = imgPic
  109.                 .WaitOnLoad = True
  110.                 .SizeMode = PictureBoxSizeMode.StretchImage
  111.             End With

  112.         Catch ex As Exception
  113.             MessageBox.Show(ex.Message)
  114.         End Try
  115.         Me.Cursor = Cursors.Default
  116.         Label1.Text = "File Upload: " & txtLocalFileName.Text
  117.     End Sub

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

  129.     Private Sub txtLocalFileName_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtLocalFileName.KeyPress
  130.         '/ Lock keypress.
  131.         e.Handled = True
  132.     End Sub

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

  138.     Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
  139.         Me.Close()
  140.     End Sub

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



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


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

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

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

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

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

GMT+7, 2024-4-30 02:02 , Processed in 0.123180 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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