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

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

[VB.NET] แจกโค้ดฟรีในการอัพโหลดไฟล์ขึ้นสู่โฮสติ้งด้วยการใช้ FTP

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

309

กระทู้

500

โพสต์

6030

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6030



สำหรับโค้ดชุดนี้ก็จะเป็นการอัพโหลดไฟล์ภาพขึ้นสู่โฮสติ้ง และให้ทำการแสดงผลจากลิ้งค์ที่รับมา โดยแอดมินเลือกใช้ฟรีโฮสติ้ง HelioHost.org ซึ่งให้บริการพื้นที่ขนาด 1 GB. และฐานข้อมูล MySQL ขนาด 1 GB. โดยไม่จำกัดจำนวนฐานข้อมูล ส่วนอื่นๆก็แทบจะไม่จำกัดการใช้งานใดๆเลย เข้าไปสมัครใช้งานกันน่ะครับ ซึ่งต่อไปแอดมินก็จะใช้โฮสตัวนี้ทำการทดสอบการเขียนโปรแกรมแบบออนไลน์ และทำการอัพโหลดภาพไปเก็บไว้ที่เดียวกัน ...

มาดูวิธีการตั้งค่า FTP Account ให้กับ Users ก่อน ...
เลือก FTP Accounts ...


กำหนดชื่อ รหัสผ่าน และตำแหน่งที่จะเก็บไฟล์ ... ขอให้สังเกต Directory ค่าที่จะถูกกำหนดดีฟอลท์มันจะอยู่ที่ public_html/g2gnet.heliohost.org/usertest  แต่แอดมินจะตัดในส่วนสีแดงทิ้งออกไป เพื่อให้ง่ายต่อการหาตำแหน่ง เวลาที่เราไปเขียนโค้ดใน VB .NET ...


การตั้งค่าล็อคอินให้กับ Users ใน FTP Client ...


ส่วนของ Root Directory ของ Users แอดมินเพิ่มโฟลเดอร์เข้าไปคือ upload ... ระวังตัวอักษรตัวเล็กตัวใหญ่ด้วยครับ เพราะนี่เป็น Linux Server มันมีผลต่อการเขียนโปรแกรม ...


Add Reference Chilkat ... คลิ๊กเพื่อดาวน์โหลด Chilkat .Net Component ที่นี่ (เฉพาะสมาชิกเท่านั้น)


มาดูโค้ดกันเถอะ ...
  1. Imports System.IO
  2. Imports System.Net
  3. Imports Chilkat.Ftp2

  4. Public Class frmFTPsample
  5.     Dim UploadFileName As String = ""  '// Full Path and File name of Image.
  6.     Dim streamPic As Stream     '// Use Steam instead IO.
  7.     Dim PicturePath As String = MyPath(Application.StartupPath) & "Images"
  8.     '// แยกชื่อไฟล์+นามสกุล เพื่อทำการ Upload ไปยัง Hosting
  9.     Dim MyPictureName As String = String.Empty
  10.     '// ลิ้งค์ที่จะทำการแสดงผลภาพ หลังจากการอัพโหลด
  11.     Dim MyURL As String = "http://g2gnet.heliohost.org/usertest/upload/"

  12.     Private Sub frmFTPsample_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  13.         Call UnlockChilkat()
  14.         '// Initialized.
  15.         '// For FreeHostia.com
  16.         txtRemoteDir.Text = "/upload/"
  17.         Label1.Text = ""
  18.         picData.Image = Image.FromFile(PicturePath + "NoImage.gif")
  19.         Label3.Text = MyURL
  20.     End Sub

  21.     Private Sub btnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
  22.         Dim dlgImage As OpenFileDialog = New OpenFileDialog()
  23.         ' / Open File Dialog
  24.         With dlgImage
  25.             '.InitialDirectory = PicturePath 'PicturePath
  26.             .Title = "เลือกภาพ"
  27.             .Filter = "รูปแบบภาพ (*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp"
  28.             .FilterIndex = 1
  29.             .RestoreDirectory = True
  30.         End With
  31.         ' Select OK after Browse ...
  32.         If dlgImage.ShowDialog() = DialogResult.OK Then
  33.             '// New Image
  34.             UploadFileName = dlgImage.FileName
  35.             ' Get file size
  36.             Dim info As New FileInfo(dlgImage.FileName)
  37.             If (info.Length / 1024) > 1024 Then
  38.                 MessageBox.Show("ไฟล์ภาพที่คุณเลือกมีขนาด " & Format((info.Length / 1024), "#,##0") & " KB. ซึ่งมีขนาดใหญ่เกินกว่า 1,024 KB.", "รายงานสถานะ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  39.                 Exit Sub
  40.             End If
  41.             Dim arr() As String = Split(dlgImage.FileName, "")
  42.             '// ตัดเอาเฉพาะชื่อไฟล์ เช่น sample.png เพื่อส่งค่าไปต่อ RemoteDir
  43.             MyPictureName = arr(UBound(arr))
  44.             '/
  45.             picData.Image = Image.FromFile(UploadFileName)
  46.             Label1.Text = UploadFileName
  47.         End If
  48.     End Sub

  49.     ' / -----------------------------------------------------------------------------
  50.     ' / Use Steam instead IO.
  51.     ' / -----------------------------------------------------------------------------
  52.     Sub ShowPicture(PicName As String)
  53.         Dim imgDB As Image
  54.         ' Get the name of the image file from the database.
  55.         If PicName.ToString <> "" Then
  56.             ' Verify that the image file meets the specified location.
  57.             If System.IO.File.Exists(PicturePath & PicName.ToString) Then
  58.                 ' Because when deleting the image file is locked, it can not be removed.
  59.                 ' The file is closed after the image is loaded, so you can delete the file if you need to
  60.                 streamPic = File.OpenRead(PicturePath & PicName.ToString)
  61.                 imgDB = Image.FromStream(streamPic)
  62.                 picData.Image = imgDB
  63.                 UploadFileName = PicName
  64.             Else
  65.                 ' No images were retrieved from the database.
  66.                 streamPic = File.OpenRead(PicturePath & "NoImage.gif")
  67.                 imgDB = Image.FromStream(streamPic)
  68.                 picData.Image = imgDB
  69.                 UploadFileName = ""
  70.             End If
  71.             ' Is null
  72.         Else
  73.             streamPic = File.OpenRead(PicturePath & "NoImage.gif")
  74.             imgDB = Image.FromStream(streamPic)
  75.             picData.Image = imgDB
  76.             UploadFileName = ""
  77.         End If
  78.         '//
  79.         streamPic.Dispose()
  80.     End Sub

  81.     Private Sub btnDeleteImg_Click(sender As System.Object, e As System.EventArgs) Handles btnDeleteImg.Click
  82.         picData.Image = Image.FromFile(PicturePath & "NoImage.gif")
  83.         UploadFileName = ""
  84.         Label1.Text = ""
  85.     End Sub

  86.     Private Sub btnUpload_Click(sender As System.Object, e As System.EventArgs) Handles btnUpload.Click
  87.         If picData.Image Is Nothing Or UploadFileName Is Nothing Or UploadFileName.Length = 0 Or UploadFileName = "" Then
  88.             MessageBox.Show("Please select the image file first.", "Report Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  89.             Return
  90.         End If
  91.         '//
  92.         Me.Cursor = Cursors.WaitCursor
  93.         '// Upload to Hosting with SFTP.
  94.         Call UploadFTP()
  95.         '// Show Image From URL.
  96.         Call ShowImgURL(MyPictureName, picURL)
  97.         Me.Cursor = Cursors.Default
  98.         '// Clear
  99.         UploadFileName = ""
  100.         picData.Image = Image.FromFile(PicturePath + "NoImage.gif")
  101.         Label1.Text = ""
  102.     End Sub

  103.     Private Sub UploadFTP()
  104.         Dim ftp As New Chilkat.Ftp2
  105.         ftp.Hostname = "ftp.g2gnet.heliohost.org"
  106.         ftp.Username = "USERNAME"
  107.         ftp.Password = "PASSWORD"
  108.         '/ Connect and login to the FTP server.
  109.         Dim success As Boolean = ftp.Connect()
  110.         If (success <> True) Then
  111.             MessageBox.Show(ftp.LastErrorText)
  112.             Exit Sub
  113.         End If
  114.         '/ Change to the remote directory where the file will be uploaded.
  115.         success = ftp.ChangeRemoteDir("upload/")
  116.         If (success <> True) Then
  117.             MessageBox.Show(ftp.LastErrorText)
  118.             Exit Sub
  119.         End If
  120.         '/ Upload a file such as "D:\123.jpg"
  121.         Dim localPath As String = UploadFileName
  122.         '// Upload to /thongkorn.com/upload/ชื่อ+นามสกุลไฟล์ภาพ
  123.         Dim remoteFilename As String = txtRemoteDir.Text.Trim & MyPictureName

  124.         success = ftp.PutFile(localPath, remoteFilename)
  125.         If (success <> True) Then
  126.             MessageBox.Show(ftp.LastErrorText)
  127.             Exit Sub
  128.         End If
  129.         success = ftp.Disconnect()
  130.         MessageBox.Show("File Uploaded Complete.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  131.     End Sub

  132.     '// Show Image From URL.
  133.     Public Sub ShowImgURL(PicName As String, ByRef picData As PictureBox)
  134.         '// Check
  135.         If Not IsValid(MyURL & PicName) Or PicName = "" Then
  136.             PicName = "NoImage.gif"
  137.         End If
  138.         Dim wClient As WebClient = New WebClient
  139.         Dim bmpImage As Bitmap = Bitmap.FromStream(New MemoryStream(wClient.DownloadData(MyURL & PicName + "?r=" + DateTime.Now.Ticks.ToString)))
  140.         Try
  141.             With picData
  142.                 .SizeMode = PictureBoxSizeMode.StretchImage
  143.                 .WaitOnLoad = True ' False
  144.                 picData.Image = bmpImage
  145.             End With
  146.         Catch ex As Exception
  147.             MessageBox.Show(ex.Message)
  148.         End Try
  149.     End Sub

  150.     '// Check Valid Image.
  151.     Public Function IsValid(ByVal Url As String) As Boolean
  152.         Dim sStream As Stream
  153.         Dim URLReq As HttpWebRequest
  154.         Dim URLRes As HttpWebResponse
  155.         Try
  156.             URLReq = WebRequest.Create(Url + "?r=" + DateTime.Now.Ticks.ToString)
  157.             URLRes = URLReq.GetResponse()
  158.             sStream = URLRes.GetResponseStream()
  159.             Dim reader As String = New StreamReader(sStream).ReadToEnd()
  160.             Return True
  161.         Catch ex As Exception
  162.             'Url not valid
  163.             Return False
  164.         End Try
  165.     End Function

  166.     Public Sub UnlockChilkat()
  167.         Dim glob As New Chilkat.Global
  168.         Dim success As Boolean = glob.UnlockBundle("")
  169.         If (success <> True) Then
  170.             MessageBox.Show(glob.LastErrorText)
  171.             Exit Sub
  172.         End If
  173.         Dim status As Integer = glob.UnlockStatus
  174.         If (status = 2) Then
  175.             'MessageBox.Show("Unlocked using purchased unlock code.")
  176.         Else
  177.             MessageBox.Show("Unlocked in trial mode.")
  178.         End If
  179.     End Sub

  180.     ' / Get my project path
  181.     ' / AppPath = C:\My Project\bin\debug
  182.     ' / Replace "\bin\debug" with ""
  183.     ' / Return : C:\My Project\
  184.     Function MyPath(ByVal AppPath As String) As String
  185.         '/ MessageBox.Show(AppPath);
  186.         AppPath = AppPath.ToLower()
  187.         '/ Return Value
  188.         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "").Replace("\bin\x86\release", "")
  189.         '// If not found folder then put the \ (BackSlash) at the end.
  190.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  191.     End Function

  192.     Private Sub frmFTPsample_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  193.         Me.Dispose()
  194.         GC.SuppressFinalize(Me)
  195.         Application.Exit()
  196.     End Sub

  197. End Class
คัดลอกไปที่คลิปบอร์ด

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

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

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

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

33

โพสต์

372

เครดิต

Full Member

Rank: 3Rank: 3

เครดิต
372
โพสต์ 2020-7-15 22:43:14 | ดูโพสต์ทั้งหมด

ขอบคุณคับ

0

กระทู้

51

โพสต์

233

เครดิต

Full Member

Rank: 3Rank: 3

เครดิต
233
โพสต์ 2020-7-16 09:22:27 | ดูโพสต์ทั้งหมด

Thankyou ขอบคุณครับ

0

กระทู้

58

โพสต์

10

เครดิต

Member

Rank: 2

เครดิต
10
โพสต์ 2022-10-25 15:06:17 | ดูโพสต์ทั้งหมด

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

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

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

GMT+7, 2024-4-20 02:28 , Processed in 0.253372 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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