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

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

[VB.NET] การ Upload/Download ไฟล์ด้วย XceedFTP ActiveX

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

311

กระทู้

502

โพสต์

6052

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6052



สำหรับโค้ด VB.NET ชุดนี้ก็เป็นการใช้งาน XceedFTP เพื่อทำการ Upload และ Download ไฟล์ภาพกราฟิค แต่แอดมินลองทดสอบด้วยการใช้ ActiveX แทน ซึ่งเป็นการนำเทคโนโลยีเก่าแบบ COM (Component Object Model) มาใช้งานกับ .Net Framework หรือที่เราเรียกว่า Interoperability ...

ดาวน์โหลด Xceed Ultimate Suite ได้ที่นี่ ... (เฉพาะสมาชิกเท่านั้น)

การเรียกใช้งาน Xceed ActiveX บน .Net Framework



มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. ' / --------------------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / More Info: http://www.g2gnet.com/webboard
  8. ' /
  9. ' / Purpose: Sample VB.Net code for Upload/Download Images to Web Hosting.
  10. ' / Microsoft Visual Basic .NET (2010) + XceedFTP ActiveX.
  11. ' /
  12. ' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
  13. ' / You can modify and/or distribute without to inform the developer.
  14. ' / --------------------------------------------------------------------------------

  15. Imports XceedFtpLib
  16. Imports System.IO

  17. Public Class frmXceedFtp
  18.     Dim streamPic As Stream     '// Use Steam instead IO.
  19.     Dim PicturePath As String = MyPath(Application.StartupPath) & "Images"
  20.     '//
  21.     Dim FullPathFileName As String = String.Empty
  22.     Dim PictureFileName As String = String.Empty
  23.     '// Constant Variables.
  24.     Const RemoteDir As String = "thongkorn.com\upload"
  25.     Const XceedFTPLicense As String = "License Key"
  26.     Const Server As String = "thongkorn.com"
  27.     Const Username As String = "USERNAME"
  28.     Const Password As String = "PASSWORD"

  29.     Private Sub btnUpload_Click(sender As System.Object, e As System.EventArgs) Handles btnUpload.Click
  30.         If Trim(FullPathFileName) = "" Or Len(FullPathFileName) = 0 Then Return
  31.         '/ The following example demonstrates how to connect to a
  32.         '/ FTP server and upload a file.
  33.         Dim ftp As New XceedFtpLib.XceedFtp
  34.         ftp.License(XceedFTPLicense)

  35.         '/ Specify the FTP server's address
  36.         ftp.ServerAddress = Server
  37.         '/ Specify the username and password to login
  38.         ftp.UserName = Username
  39.         ftp.Password = Password

  40.         Try
  41.             Me.Cursor = Cursors.WaitCursor
  42.             '/ Connect to the FTP server
  43.             ftp.Connect()

  44.             '/ Send a file
  45.             ftp.SendFile(FullPathFileName, 0, RemoteDir & PictureFileName, False)

  46.             '/ Disconnect from the FTP server
  47.             ftp.Disconnect()
  48.             MessageBox.Show("Upload file successfully.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  49.         Catch except As System.Runtime.InteropServices.COMException
  50.             MessageBox.Show(except.ToString())
  51.         End Try
  52.         Me.Cursor = Cursors.Default
  53.     End Sub

  54.     Private Sub btnDownload_Click(sender As System.Object, e As System.EventArgs) Handles btnDownload.Click
  55.         '// Trap Error.
  56.         If Trim(FullPathFileName) = "" Or Len(FullPathFileName) = 0 Then Return
  57.         '/ The following example demonstrates how to connect to a
  58.         '/ FTP server and download a file.
  59.         Dim ftp As New XceedFtpLib.XceedFtp
  60.         ftp.License(XceedFTPLicense)

  61.         '/ Specify the FTP server's address
  62.         ftp.ServerAddress = Server
  63.         '/ Specify the username and password to login
  64.         ftp.UserName = Username
  65.         ftp.Password = Password

  66.         Try
  67.             Me.Cursor = Cursors.WaitCursor
  68.             '/ Connect to the FTP server
  69.             ftp.Connect()

  70.             '/ Receive files
  71.             ftp.ReceiveFile(RemoteDir & PictureFileName, 0, MyPath(Application.StartupPath) & "Download" & PictureFileName)

  72.             '/ Disconnect from the FTP server
  73.             ftp.Disconnect()

  74.             MessageBox.Show("Download file successfully.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  75.         Catch except As System.Runtime.InteropServices.COMException
  76.             '// Most problems are the location of the file does not match. Or does not have a specified files.
  77.             MessageBox.Show(except.ToString())
  78.         End Try
  79.         Me.Cursor = Cursors.Default
  80.     End Sub

  81.     Private Sub frmXceedFtp_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  82.         picData.Image = Image.FromFile(PicturePath & "NoImage.gif")
  83.     End Sub

  84.     Private Sub btnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
  85.         Dim dlgImage As OpenFileDialog = New OpenFileDialog()

  86.         ' / Open File Dialog
  87.         With dlgImage
  88.             '.InitialDirectory = PicturePath 'PicturePath
  89.             .Title = "Select your image file"
  90.             .Filter = "Image (*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp"
  91.             .FilterIndex = 1
  92.             .RestoreDirectory = True
  93.         End With
  94.         '/ Select OK after Browse ...
  95.         If dlgImage.ShowDialog() = DialogResult.OK Then
  96.             FullPathFileName = dlgImage.FileName
  97.             '// Get only Filename & Extension.
  98.             Dim arr() As String = Split(FullPathFileName, "")
  99.             PictureFileName = arr(UBound(arr))
  100.             '/
  101.             picData.Image = Image.FromFile(FullPathFileName)
  102.         End If

  103.     End Sub

  104.     Private Sub btnDeleteImg_Click(sender As System.Object, e As System.EventArgs) Handles btnDeleteImg.Click
  105.         picData.Image = Image.FromFile(PicturePath & "NoImage.gif")
  106.         PictureFileName = String.Empty
  107.     End Sub

  108.     ' / -----------------------------------------------------------------------------
  109.     ' / Use Steam instead IO.
  110.     ' / -----------------------------------------------------------------------------
  111.     Sub ShowPicture(PicName As String)
  112.         Dim imgDB As Image
  113.         ' Get the name of the image file.
  114.         If PicName.ToString <> "" Then
  115.             ' Verify that the image file meets the specified location.
  116.             If System.IO.File.Exists(PicturePath & PicName.ToString) Then
  117.                 '/ Because when deleting the image file is locked, it can not be removed.
  118.                 '/ The file is closed after the image is loaded, so you can delete the file if you need.
  119.                 streamPic = File.OpenRead(PicturePath & PicName.ToString)
  120.                 imgDB = Image.FromStream(streamPic)
  121.                 picData.Image = imgDB
  122.             Else
  123.                 '/ No images.
  124.                 streamPic = File.OpenRead(PicturePath & "NoImage.gif")
  125.                 imgDB = Image.FromStream(streamPic)
  126.                 picData.Image = imgDB
  127.             End If

  128.             ' Is null
  129.         Else
  130.             streamPic = File.OpenRead(PicturePath & "NoImage.gif")
  131.             imgDB = Image.FromStream(streamPic)
  132.             picData.Image = imgDB
  133.         End If
  134.         '//
  135.         streamPic.Dispose()
  136.     End Sub

  137.     ' / Get my project path
  138.     ' / AppPath = C:\My Project\bin\debug
  139.     ' / Replace "\bin\debug" with ""
  140.     ' / Return : C:\My Project\
  141.     Function MyPath(ByVal AppPath As String) As String
  142.         '/ MessageBox.Show(AppPath);
  143.         AppPath = AppPath.ToLower()
  144.         '/ Return Value
  145.         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "").Replace("\bin\x86\release", "")
  146.         '// If not found folder then put the \ (BackSlash) at the end.
  147.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  148.     End Function

  149.     Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
  150.         Me.Close()
  151.     End Sub

  152.     Private Sub frmXceedFtp_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  153.         Me.Dispose()
  154.         Application.Exit()
  155.     End Sub

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

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

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

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

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

0

กระทู้

33

โพสต์

372

เครดิต

Full Member

Rank: 3Rank: 3

เครดิต
372
โพสต์ 2019-10-29 23:22:22 | ดูโพสต์ทั้งหมด

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

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

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

GMT+7, 2024-4-26 07:33 , Processed in 0.133624 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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