|  | 
 
|  
 สำหรับโค้ด VB.NET ชุดนี้ก็เป็นการใช้งาน XceedFTP เพื่อทำการ Upload และ Download ไฟล์ภาพกราฟิค แต่แอดมินลองทดสอบด้วยการใช้ ActiveX แทน ซึ่งเป็นการนำเทคโนโลยีเก่าแบบ COM (Component Object Model) มาใช้งานกับ .Net Framework หรือที่เราเรียกว่า Interoperability ...
 
 ดาวน์โหลด Xceed Ultimate Suite ได้ที่นี่ ... (เฉพาะสมาชิกเท่านั้น)
 
 การเรียกใช้งาน Xceed ActiveX บน .Net Framework
 
  
 
  มาดูโค้ดฉบับเต็มกันเถอะ ...
 
 คัดลอกไปที่คลิปบอร์ด' / --------------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / More Info: http://www.g2gnet.com/webboard
' /
' / Purpose: Sample VB.Net code for Upload/Download Images to Web Hosting.
' / Microsoft Visual Basic .NET (2010) + XceedFTP ActiveX.
' /
' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / --------------------------------------------------------------------------------
Imports XceedFtpLib
Imports System.IO
Public Class frmXceedFtp
    Dim streamPic As Stream     '// Use Steam instead IO.
    Dim PicturePath As String = MyPath(Application.StartupPath) & "Images"
    '//
    Dim FullPathFileName As String = String.Empty
    Dim PictureFileName As String = String.Empty
    '// Constant Variables.
    Const RemoteDir As String = "thongkorn.com\upload"
    Const XceedFTPLicense As String = "License Key"
    Const Server As String = "thongkorn.com"
    Const Username As String = "USERNAME"
    Const Password As String = "PASSWORD"
    Private Sub btnUpload_Click(sender As System.Object, e As System.EventArgs) Handles btnUpload.Click
        If Trim(FullPathFileName) = "" Or Len(FullPathFileName) = 0 Then Return
        '/ The following example demonstrates how to connect to a
        '/ FTP server and upload a file.
        Dim ftp As New XceedFtpLib.XceedFtp
        ftp.License(XceedFTPLicense)
        '/ Specify the FTP server's address
        ftp.ServerAddress = Server
        '/ Specify the username and password to login
        ftp.UserName = Username
        ftp.Password = Password
        Try
            Me.Cursor = Cursors.WaitCursor
            '/ Connect to the FTP server
            ftp.Connect()
            '/ Send a file
            ftp.SendFile(FullPathFileName, 0, RemoteDir & PictureFileName, False)
            '/ Disconnect from the FTP server
            ftp.Disconnect()
            MessageBox.Show("Upload file successfully.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch except As System.Runtime.InteropServices.COMException
            MessageBox.Show(except.ToString())
        End Try
        Me.Cursor = Cursors.Default
    End Sub
    Private Sub btnDownload_Click(sender As System.Object, e As System.EventArgs) Handles btnDownload.Click
        '// Trap Error.
        If Trim(FullPathFileName) = "" Or Len(FullPathFileName) = 0 Then Return
        '/ The following example demonstrates how to connect to a
        '/ FTP server and download a file.
        Dim ftp As New XceedFtpLib.XceedFtp
        ftp.License(XceedFTPLicense)
        '/ Specify the FTP server's address
        ftp.ServerAddress = Server
        '/ Specify the username and password to login
        ftp.UserName = Username
        ftp.Password = Password
        Try
            Me.Cursor = Cursors.WaitCursor
            '/ Connect to the FTP server
            ftp.Connect()
            '/ Receive files
            ftp.ReceiveFile(RemoteDir & PictureFileName, 0, MyPath(Application.StartupPath) & "Download" & PictureFileName)
            '/ Disconnect from the FTP server
            ftp.Disconnect()
            MessageBox.Show("Download file successfully.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch except As System.Runtime.InteropServices.COMException
            '// Most problems are the location of the file does not match. Or does not have a specified files.
            MessageBox.Show(except.ToString())
        End Try
        Me.Cursor = Cursors.Default
    End Sub
    Private Sub frmXceedFtp_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        picData.Image = Image.FromFile(PicturePath & "NoImage.gif")
    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
            '.InitialDirectory = PicturePath 'PicturePath
            .Title = "Select your image file"
            .Filter = "Image (*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp"
            .FilterIndex = 1
            .RestoreDirectory = True
        End With
        '/ Select OK after Browse ...
        If dlgImage.ShowDialog() = DialogResult.OK Then
            FullPathFileName = dlgImage.FileName
            '// Get only Filename & Extension.
            Dim arr() As String = Split(FullPathFileName, "")
            PictureFileName = arr(UBound(arr))
            '/
            picData.Image = Image.FromFile(FullPathFileName)
        End If
    End Sub
    Private Sub btnDeleteImg_Click(sender As System.Object, e As System.EventArgs) Handles btnDeleteImg.Click
        picData.Image = Image.FromFile(PicturePath & "NoImage.gif")
        PictureFileName = String.Empty
    End Sub
    ' / -----------------------------------------------------------------------------
    ' / Use Steam instead IO.
    ' / -----------------------------------------------------------------------------
    Sub ShowPicture(PicName As String)
        Dim imgDB As Image
        ' Get the name of the image file.
        If PicName.ToString <> "" Then
            ' Verify that the image file meets the specified location.
            If System.IO.File.Exists(PicturePath & PicName.ToString) Then
                '/ Because when deleting the image file is locked, it can not be removed.
                '/ The file is closed after the image is loaded, so you can delete the file if you need.
                streamPic = File.OpenRead(PicturePath & PicName.ToString)
                imgDB = Image.FromStream(streamPic)
                picData.Image = imgDB
            Else
                '/ No images.
                streamPic = File.OpenRead(PicturePath & "NoImage.gif")
                imgDB = Image.FromStream(streamPic)
                picData.Image = imgDB
            End If
            ' Is null
        Else
            streamPic = File.OpenRead(PicturePath & "NoImage.gif")
            imgDB = Image.FromStream(streamPic)
            picData.Image = imgDB
        End If
        '//
        streamPic.Dispose()
    End Sub
    ' / Get my project path
    ' / AppPath = C:\My Project\bin\debug
    ' / Replace "\bin\debug" with ""
    ' / Return : C:\My Project\
    Function MyPath(ByVal AppPath As String) As String
        '/ MessageBox.Show(AppPath);
        AppPath = AppPath.ToLower()
        '/ Return Value
        MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "").Replace("\bin\x86\release", "")
        '// If not found folder then put the \ (BackSlash) at the end.
        If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
    End Function
    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
    Private Sub frmXceedFtp_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
        Application.Exit()
    End Sub
End Class
ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2010) ได้ที่นี่ ...
 
 
 | 
 
xขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึงคุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน  |