silanos_1

Analytics

ad silanors2

2010年7月23日 星期五

[FTP]如何 依傳入資料夾名稱 上傳檔案到FTP(By passing the folder name to upload files to FTP)


問題

如何 依傳入資料夾名稱 上傳檔案到FTP



解決方法

這支function只要在處理上傳檔案到ftp,若有給資料夾名稱,則傳到指定資料夾,若無傳資料夾名稱,則傳檔案到ftp根目錄

程式碼如下:
Public Shared Function PushFiles2FTP(ByVal filePathAs String, ByVal fileNameAs String)
'fileName上傳的檔案ex : c:\abc.xml ,
'FTP_SiteName 上傳的FTP伺服器IP
'FTP_UserName 使用者FTP登入帳號 ,
'FTP_Password使用者登入密碼
'127.0.0.1

Dim FTP_SiteName As String = System.Configuration.ConfigurationSettings.AppSettings.Get("FTP_SiteName").ToString
'admin
Dim FTP_UserName As String = System.Configuration.ConfigurationSettings.AppSettings.Get("FTP_UserName").ToString
'123
Dim FTP_Password As String = System.Configuration.ConfigurationSettings.AppSettings.Get("FTP_Password").ToString

Dim requestStream As Stream = Nothing
Dim fileStream As FileStream = Nothing
Dim uploadResponse As FtpWebResponse = Nothing

Dim pathlocal As String = HttpContext.Current.Server.MapPath("..\down\") '暫存路徑固定為down底下
pathlocal += fileName

        Try
            Dim Address As String = "ftp://" & FTP_SiteName & "/" & fileName
            If filePath= String.Empty Then
                Address = "ftp://" & FTP_SiteName & "/" & fileName
            Else
                Address = "ftp://" & FTP_SiteName & "/" & filePath& "/" & fileName
            End If

            Dim uploadRequest As FtpWebRequest
            If PI_FTP_PushPath <> String.Empty Then
                Try
                    uploadRequest = DirectCast(WebRequest.Create(New Uri("ftp://" & FTP_SiteName & "/" & filePath)), FtpWebRequest)
     If FTP_UserName.Length > 0 Then
      '如果需要帳號登入
      Dim nc As New NetworkCredential(FTP_UserName, FTP_Password)
     '設定帳號
      uploadRequest.Credentials = nc
     End If
                   '若有傳資料夾,則是著建立,若建立錯誤,則代表已有資料夾
                    uploadRequest.Method = WebRequestMethods.Ftp.MakeDirectory
                    uploadRequest.UseBinary = True

                    Dim response As FtpWebResponse = uploadRequest.GetResponse()
                    Dim ftpStream = response.GetResponseStream()

                    ftpStream.Close()
                    response.Close()
                Catch ex As Exception
                End Try
            End If
            uploadRequest = DirectCast(WebRequest.Create(Address), FtpWebRequest)
   If FTP_UserName.Length > 0 Then
                '如果需要帳號登入
                Dim nc As New NetworkCredential(FTP_UserName, FTP_Password)
               '設定帳號
                uploadRequest.Credentials = nc
            End If

            uploadRequest.Method = WebRequestMethods.Ftp.UploadFile
           '設定Method上傳檔案
            uploadRequest.Proxy = Nothing
            requestStream = uploadRequest.GetRequestStream()

            fileStream = File.Open(pathlocal, FileMode.Open)
            Dim buffer As Byte() = New Byte(1024) {}
            Dim bytesRead As Integer
            While True
                '開始上傳資料流
                bytesRead = fileStream.Read(buffer, 0, buffer.Length)
    If bytesRead = 0 Then
                   Exit While
    End If
                requestStream.Write(buffer, 0, bytesRead)
   End While

            requestStream.Close()
            uploadResponse = DirectCast(uploadRequest.GetResponse(), FtpWebResponse)

            fileStream.Flush()
            fileStream.Close()
        Catch ex As Exception

        Finally
            If uploadResponse IsNot Nothing Then
                uploadResponse.Close()
            End If
            If fileStream IsNot Nothing Then
                fileStream.Close()
            End If
   If requestStream IsNot Nothing Then
                requestStream.Close()
   End If
            '刪除檔案
            If PO_ERRCDE <> 99 Then
                Dim delFile As File
                delFile.Delete(pathlocal)
            End If
        End Try

    End Function

12 則留言:

匿名 提到...

請問一下影片也可以依照以上的流程而上傳到FTP嗎?所要上傳的檔案名稱輸入請問在哪一行

silano.chen 提到...

可以的!
呼叫function時傳入fileName,而此filesname是完整的路徑!
ex : c:\abc.xml,
另外程式執行的資料夾要建立名為down的資料夾,以暫存檔案,當然資料夾名稱也可以自己改名

Rex 提到...

不好意思
小的不才
要打上這些CODE之前
是不是要先IMPORT些甚麼東西進去
還有一些類別是否要先宣告?
我是上次發問的人

silano.chen 提到...

IMPORT System.IO;
IMPORT System.Net;

另外mark掉兩行判斷

匿名 提到...

您好
Dim pathlocal As String = HttpContext.Current.Server.MapPath("..\down\")
沒辦法使用
我適用VB2008的版本
不知道大大是否可以幫我一下~!!

silano.chen 提到...

你好!錯誤訊息是什麼?

匿名 提到...

名稱'HttpContext'未宣告

silano.chen 提到...

缺少Imports System.Net

匿名 提到...

在最上面有輸入
可是編譯上就會那段出現錯誤
不給編譯
不知道是什麼問題
不好意思一直麻煩您

http://www.badongo.com/file/24404536

附上程式碼給您參考
謝謝您

silano.chen 提到...

因為你開的不是WEB專案,而是Winform專案,所以要加入參考
專案檔右鍵=>加入參考=>選.net=>找System.Net=>ok

匿名 提到...

上傳的檔案封包怎麼加密?

silano.chen 提到...

uploadRequest.UseBinary = true;
這可以將資料傳成binary,除非整個擷取,否則資料會不完整

as silanors3

熱門文章

bloggerads