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

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

[VB.NET] แจกโค้ดฟรีการอัพโหลดไฟล์ขึ้นสู่ Web Hosting ด้วย Component มาตรฐานของ MS

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

310

กระทู้

501

โพสต์

6035

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6035



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

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

  3. Public Class frmFTPClientNet
  4.     '// Get only Filename + Extension
  5.     Dim sFileName As String
  6.     Dim RemoteDir As String = "ftp://ftp.YOUR_DNS.heliohost.org//upload/"
  7.     Dim UName As String = "USERNAME@YOUR_DNS.heliohost.org"
  8.     Dim Pwd As String = "PASSWORD"

  9.     Private Sub btnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
  10.         ' Declare Open File Dialog Control - Run Time
  11.         Dim dlgImage As OpenFileDialog = New OpenFileDialog()

  12.         Dim strPath As String = MyPath(Application.StartupPath) & "Images"
  13.         ' / Initialize Open File Dialog
  14.         With dlgImage
  15.             .InitialDirectory = strPath & "Images"
  16.             .Title = "Choose images - Images File Format"
  17.             .Filter = "Images (*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp"
  18.             .FilterIndex = 1
  19.             .RestoreDirectory = True
  20.         End With
  21.         ' Select OK
  22.         If dlgImage.ShowDialog() = DialogResult.OK Then
  23.             ' Get file size less than 1,024 KB.
  24.             Dim info As New FileInfo(dlgImage.FileName)
  25.             If (info.Length / 1024) > 1024 Then
  26.                 MessageBox.Show("Picture file size has " & Format((info.Length / 1024), "#,##0") & " KB. too large 1,024 KB.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  27.                 Exit Sub
  28.             End If
  29.             '//
  30.             txtFileName.Text = dlgImage.FileName
  31.             '// Put the current image file into PictureBox Control
  32.             picData.Image = Image.FromFile(dlgImage.FileName)
  33.             '// Get only Filename + Extension. Ex. thongkorn.png
  34.             sFileName = dlgImage.SafeFileName
  35.         End If
  36.     End Sub

  37.     Private Sub btnUploadFile1_Click(sender As System.Object, e As System.EventArgs) Handles btnUploadFile1.Click
  38.         If txtFileName.Text.Trim.Length = 0 Then Return
  39.         Me.Cursor = Cursors.WaitCursor
  40.         Try
  41.             '/ Create Request To Upload File
  42.             Dim wrUpload As FtpWebRequest = DirectCast(WebRequest.Create(RemoteDir & sFileName), FtpWebRequest)
  43.             '/ Specify UName & Pwd
  44.             wrUpload.Credentials = New NetworkCredential(UName, Pwd)
  45.             '/ Start Upload Process
  46.             wrUpload.Method = WebRequestMethods.Ftp.UploadFile
  47.             '/ Locate File And Store It In Byte Array
  48.             Dim BitFile() As Byte = File.ReadAllBytes(txtFileName.Text)
  49.             '/ Get File'
  50.             Dim strFile As Stream = wrUpload.GetRequestStream()
  51.             '/ Upload Each Byte
  52.             strFile.Write(BitFile, 0, BitFile.Length)
  53.             '/ Close
  54.             strFile.Close()
  55.             '/ Free Memory
  56.             strFile.Dispose()
  57.             MessageBox.Show("File " & sFileName & " Uploaded Complete.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  58.         Catch ex As Exception
  59.             MessageBox.Show(ex.Message)
  60.         Finally
  61.             txtFileName.Clear()
  62.             picData.Image = Nothing
  63.         End Try
  64.         Me.Cursor = Cursors.Default
  65.     End Sub

  66.     Private Sub btnUploadFile2_Click(sender As System.Object, e As System.EventArgs) Handles btnUploadFile2.Click
  67.         If txtFileName.Text.Trim.Length = 0 Then Return
  68.         Me.Cursor = Cursors.WaitCursor
  69.         Try
  70.             Dim Request As FtpWebRequest = WebRequest.Create(RemoteDir & sFileName)
  71.             '// Login
  72.             Request.Credentials = New NetworkCredential(UName, Pwd)
  73.             Request.Method = WebRequestMethods.Ftp.UploadFile
  74.             Using FileStream As Stream = File.OpenRead(txtFileName.Text),
  75.                   ftpStream As Stream = Request.GetRequestStream()
  76.                 Dim read As Integer
  77.                 Do
  78.                     Dim Buffer() As Byte = New Byte(10240) {}
  79.                     read = FileStream.Read(Buffer, 0, Buffer.Length)
  80.                     If read > 0 Then
  81.                         ftpStream.Write(Buffer, 0, read)
  82.                         Console.WriteLine("Uploaded {0} bytes", FileStream.Position)
  83.                     End If
  84.                 Loop While read > 0
  85.             End Using
  86.             MessageBox.Show("File " & sFileName & " Uploaded Complete.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  87.             '//
  88.         Catch ex As Exception
  89.             MessageBox.Show(ex.Message)
  90.         Finally
  91.             txtFileName.Clear()
  92.             picData.Image = Nothing
  93.         End Try
  94.         Me.Cursor = Cursors.Default
  95.     End Sub

  96.     Private Sub txtFileName_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtFileName.KeyPress
  97.         e.Handled = True
  98.     End Sub

  99.     ' / --------------------------------------------------------------------------------
  100.     ' / Get my project path
  101.     ' / AppPath = C:\My Project\bin\debug
  102.     ' / Replace "\bin\debug" with ""
  103.     ' / Return : C:\My Project\
  104.     Function MyPath(AppPath As String) As String
  105.         '/ MessageBox.Show(AppPath);
  106.         AppPath = AppPath.ToLower()
  107.         '/ Return Value
  108.         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  109.         '// If not found folder then put the \ (BackSlash ASCII Code = 92) at the end.
  110.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  111.     End Function

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

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

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

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

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

0

กระทู้

33

โพสต์

372

เครดิต

Full Member

Rank: 3Rank: 3

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

ขอบคุณคับ

0

กระทู้

51

โพสต์

233

เครดิต

Full Member

Rank: 3Rank: 3

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

ขอบคุณครับอาจารย์

0

กระทู้

58

โพสต์

10

เครดิต

Member

Rank: 2

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

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

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

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

GMT+7, 2024-4-20 21:47 , Processed in 0.211038 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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