|
โค้ดชุดนี้แอดมินใช้ Chilkat.NET ในการอัพโหลดไฟล์ภาพ และทดสอบกับฟรีเว็บโฮสติ้ง www.freehostia.com ก็ลองไปสมัครใช้ฟรีกันดูน่ะครับ ...
Add References ...
มาดูโค้ดกันเถอะ ...
- Imports System.Net
- Imports System.IO
- Imports Chilkat.Ftp2
- Public Class frmUploadImageURL
- Dim MyURL As String = "http://www.thongkorn.com/upload/"
- Dim RemoteDir = "thongkorn.com/upload/"
- '//
- Dim picFileName As String = String.Empty
- Dim picFullPath As String = String.Empty
- Dim LocalFolder As String = String.Empty
- Private Sub frmUploadImageURL_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
- '// สั่งปลดล็อคก่อนใช้งาน
- Call UnlockChilkat()
- End Sub
- '// นำภาพจาก URL มาแสดงผล
- Public Sub ShowImgURL()
- Try
- Dim wc As New WebClient
- Dim imgByte() As Byte = wc.DownloadData(txtRemote.Text)
- Dim imgStream As New MemoryStream(imgByte)
- With picRemote
- .SizeMode = PictureBoxSizeMode.StretchImage
- .WaitOnLoad = True ' False
- .Image = Image.FromStream(imgStream)
- End With
- Catch ex As Exception
- MessageBox.Show(ex.Message)
- End Try
- End Sub
- Private Sub btnUpload_Click(sender As System.Object, e As System.EventArgs) Handles btnUpload.Click
- If txtLocal.Text.Trim = "" Or txtLocal.Text.Length = 0 Then Return
- '//
- Call UploadImageToURL()
- txtRemote.Text = MyURL & picFileName
- '//
- Call ShowImgURL()
- End Sub
- Private Sub btnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
- Dim dlgImage As OpenFileDialog = New OpenFileDialog()
- ' / Open File Dialog
- With dlgImage
- .Title = "Select images"
- .Filter = "Images types (*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp"
- .FilterIndex = 1
- .RestoreDirectory = False 'True
- End With
- ' Select OK after Browse ...
- If dlgImage.ShowDialog() = DialogResult.OK Then
- Dim img As New Bitmap(dlgImage.FileName)
- If img.Size.Width > 600 Or img.Size.Height > 600 Then
- MessageBox.Show("รูปภาพต้องมีขนาดกว้างและยาวไม่เกิน 600 พิกเซล.", "รายงานความผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Information)
- Exit Sub
- End If
- '// แยกโฟลเดอร์ และ ชื่อไฟล์
- Dim iArr() As String = {}
- iArr = Split(dlgImage.FileName, "")
- For i = 0 To UBound(iArr) - 1
- LocalFolder = LocalFolder & iArr(i) & ""
- Next
- '// Get Local folder
- If Microsoft.VisualBasic.Right(LocalFolder, 1) <> "" Then LocalFolder += ""
- '// Get only Filename.
- picFileName = iArr(UBound(iArr))
- '// Image
- picFullPath = dlgImage.FileName
- txtLocal.Text = picFullPath
- picLocal.Image = Image.FromFile(picFullPath)
- End If
- End Sub
- Public Sub UploadImageToURL()
- If txtLocal.Text.Trim = "" Or IsNothing(txtLocal.Text.Trim) Then Return
- '//
- Dim ftp As New Chilkat.Ftp2
- ftp.Hostname = "thongkorn.com"
- ftp.Username = "FTP USERNAME"
- ftp.Password = "FTP PASSWORD"
- '/ Connect and login to the FTP server.
- Dim success As Boolean = ftp.Connect()
- If (success <> True) Then
- MessageBox.Show(ftp.LastErrorText)
- Exit Sub
- End If
- '/ Change to the remote directory where the file will be uploaded.
- success = ftp.ChangeRemoteDir(RemoteDir)
- If (success <> True) Then
- MessageBox.Show(ftp.LastErrorText)
- Exit Sub
- End If
- '/ Upload a file.
- 'picFullPath = --> "c:/temp/hamlet.xml"
- 'picFileName = --> "hamlet.xml"
- success = ftp.PutFile(picFullPath, picFileName)
- If (success <> True) Then
- MessageBox.Show(ftp.LastErrorText)
- Exit Sub
- End If
- success = ftp.Disconnect()
- MessageBox.Show("File Uploaded Successfully.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
- End Sub
- '// Unlock Chilkat.
- Public Sub UnlockChilkat()
- Dim glob As New Chilkat.Global
- Dim success As Boolean = glob.UnlockBundle("CHILKAT KEY")
- If (success <> True) Then
- MessageBox.Show(glob.LastErrorText)
- Exit Sub
- End If
- Dim status As Integer = glob.UnlockStatus
- If (status = 2) Then
- 'MessageBox.Show("Unlocked using purchased unlock code.")
- Else
- MessageBox.Show("Unlocked in trial mode.")
- End If
- End Sub
- Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
- picLocal.Image = Nothing
- picFileName = Nothing
- txtLocal.Text = ""
- txtRemote.Text = ""
- End Sub
- Private Sub txtLocal_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtLocal.KeyPress
- e.Handled = True
- End Sub
- Private Sub txtRemote_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtRemote.KeyPress
- e.Handled = True
- End Sub
- End Class
คัดลอกไปที่คลิปบอร์ด
คลิ๊กดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...
|
|