開始囉!!!
好的!居裡貓接著說說怎麼上傳檔案到 FTP 上面啦!
這次是真的 FTP 不是模擬的資料夾上傳啊!
這邊就需要到不一樣的東西來輔助我們了~
居裡貓還沒厲害到有辦法自己寫出個 FTP Server 這種東西啦~
所以用現成的就好了~那就是它~~:
FileZilla Server Interface (←點擊轉到官網下載頁面)
那至於如何架設屬於自己的 FTP Server ,這個就請各位自己去餵狗了~~
好的,大家都設定好 FTP Server 之後呢!
我們就會有三個新的東西~就是~
FTP Address
FTP Account
Account Password
大家可以回顧一下 前情提要 居裡貓在小程式的下面有三個 TextBox 可以放上面的三個資訊!
有了伺服器,有了網址、帳號和密碼,那我們就來看程式啦!
GO~~~~~~~~~~~~
-----------------------------------------------------------------------------------------------------------------------
- '這次程式依然跨足了好幾個地方~大家在看的時要注意~
- '在 Write XML Button 裡面是要取得 FTP 上傳所需要的三個資訊~
- 'Button Contorl XML Write
- Private Sub Btn_WriteXML_Click(sender As Object, e As EventArgs) Handles Btn_WriteXML.Click
- 'Check watch event was raising
- If Watcher.watcher.EnableRaisingEvents = False Then
- Watcher.watcher.EnableRaisingEvents = True
- End If
- Copy_Delete_File.GetPath(TxB_Local.Text, TxB_Remote.Text, TxB_NAS.Text, TxB_FTP.Text, ChB_UploadFTP.Checked)
- '在這裡取得三個 FTP 所需資訊
- FTPupload.Get_FTPinfo(TxB_FTPAddress.Text, TxB_FTPAccount.Text, Txb_FTPPassword.Text)
- Dim wrXML As New XML
- wrXML.LocalPath = TxB_Local.Text
- wrXML.RemotePath = TxB_Remote.Text
- wrXML.NASPath = TxB_NAS.Text
- wrXML.FTPPath = TxB_FTP.Text
- wrXML.Data = "This is a Programming Practices " & Now.ToString
- wrXML.DateTime = Now.ToString
- If ChB_UploadFTP.Checked Then
- wrXML.CheckFTP = "Ture"
- Else
- wrXML.CheckFTP = "Flase"
- End If
- Dim objectStreamWriter As New StreamWriter(TxB_Local.Text + ("\Program Practices " & Now.ToString("_yyyyMMddHHmmss") & ".xml"))
- Dim xmlserialize As New XmlSerializer(wrXML.GetType)
- xmlserialize.Serialize(objectStreamWriter, wrXML)
- objectStreamWriter.Dispose()
- 'Check backwork status
- 'If Not back.IsBusy = True Then
- ' back.RunWorkerAsync()
- 'End If
- Backwork.main()
- End Sub
- '---分割線,上面取得資訊,下面是在需要的時間點啟動 FTP 上傳
- 'About Copy and Delete file
- Public Class Copy_Delete_File
- 'Parameter use for down below work
- Public Shared filename As String 'use get file name
- Public Shared sourcepath As String 'use get soure file path
- Public Shared copypath As String 'use get copy file path
- Public Shared uptonas As String 'use get upload to nas path
- Public Shared uptoftp As String 'use get upload to ftp path
- Public Shared checkbox As Boolean 'use get checkbox condition
- 'Get now file name
- Public Shared Sub GetFileName(Name As String)
- Console.WriteLine(Name & " Get")
- filename = Name
- End Sub
- 'Get path to use copy file, delete file, upload file
- Public Shared Sub GetPath(source As String, copyformpath As String, upNASpath As String, upFTPpath As String, check As Boolean)
- Console.WriteLine(source & vbCr & copyformpath & vbCr & upNASpath & vbCr & upFTPpath)
- sourcepath = source
- copypath = copyformpath
- uptonas = upNASpath
- uptoftp = upFTPpath
- checkbox = check
- End Sub
- 'Do copy file to remote folder, upload file to NAS folder and FTP folder then deleted local file
- Public Shared Sub copyfile()
- Try
- 'Copy file to remote folder from local folder
- File.Copy(sourcepath + "\" + filename, copypath + "\" + filename, True)
- 'Control retry parameter
- Dim Retry As Boolean = True
- Dim retrytimes As Integer = 0
- 'Check upload to NAS or FTP
- If checkbox = False Then
- While Retry
- Try
- 'Upload to NAS, copy file from remote folder, upload done delete loacal file
- File.Copy(copypath + "\" + filename, uptonas + "\" + filename, True)
- System.Threading.Thread.Sleep(100)
- Retry = False
- Catch ex As Exception
- 'Upload to NAS Error, do retry upload file and count retry times
- If retrytimes <= 10 Then
- Retry = True
- retrytimes += 1
- 'Console.WriteLine("Upload to NAS was Error, do retry upload, retry times : " & retrytimes)
- ErrorList.AddLog(Now.ToString("yyyyMMddHHmmss _ ") + "Upload to NAS was Error, do retry upload, retry times : " & retrytimes)
- System.Threading.Thread.Sleep(500)
- Else
- Retry = False
- retrytimes = 0
- 'Console.WriteLine("Retry upload to NAS times was arrived")
- 'When retry arrived then add upload error filename into list and error log list
- ErrorList.AddNas(filename)
- ErrorList.AddLog(Now.ToString("yyyyMMddHHmmss _ ") + "Retry upload to NAS times was arrived")
- End If
- End Try
- End While
- File.Delete(sourcepath + "\" + filename)
- Else
- While Retry
- Try
- 'Upload to FTP, copy file from remote folder, upload done delete loacal file
- File.Copy(copypath + "\" + filename, uptoftp + "\" + filename, True)
- System.Threading.Thread.Sleep(100)
- 'FTP 上傳當然是要在複製檔案給 FTP 的時候啟動它阿~
- FTPupload.uploadtoftp()
- System.Threading.Thread.Sleep(100)
- Retry = False
- Catch ex As Exception
- 'Upload to FTP Error, do retry upload file and count retry times
- If retrytimes <= 10 Then
- Retry = True
- retrytimes += 1
- 'Console.WriteLine("Upload to FTP was Error, do retry upload, retry times : " & retrytimes)
- ErrorList.AddLog(Now.ToString("yyyyMMddHHmmss _ ") + "Upload to FTP was Error, do retry upload, do retry upload, retry times : " & retrytimes)
- System.Threading.Thread.Sleep(500)
- Else
- Retry = False
- retrytimes = 0
- 'Console.WriteLine("Retry upload to FTP times was arrived")
- 'When retry arrived then add upload error filename into list and error log list
- ErrorList.AddFtp(filename)
- ErrorList.AddLog(Now.ToString("yyyyMMddHHmmss _ ") + "Retry upload to FTP times was arrived")
- End If
- End Try
- End While
- File.Delete(sourcepath + "\" + filename)
- End If
- Catch ex As Exception
- ErrorList.AddLog(Now.ToString("yyyyMMddHHmmss _ ") + "Copy to reomte folder failure")
- End Try
- End Sub
- End Class
- '下面開始才是 FTP 上傳的 Class
- 'About upload to FTP
- Public Class FTPupload
- 'Parameter usw for down below work
- Public Shared FTP_IP As String 'use get IP
- Public Shared FTP_Account As String 'use get account
- Public Shared FTP_Password As String 'use get password
- '這邊有個取得資訊用的程式段落
- 'Get FTP info, IP, account and password
- Public Shared Sub Get_FTPinfo(ByVal ip As String, ByVal account As String, ByVal password As String)
- FTP_IP = ip
- FTP_Account = account
- FTP_Password = password
- End Sub
- '這邊開始做 FTP 上傳時需要的工作
- 'Do upload work
- Public Shared Sub uploadtoftp()
- Dim ftpRequestStream As System.IO.Stream
- Dim statusDescription As String
- Dim retry As Boolean = True
- Dim retrytimes As Integer = 0
- '這邊一樣為了防止 FTP 上傳時失敗 ,所以加入重新上傳的機制
- While retry
- 'Create file
- Try
- 'Create FTP IP and upload file name
- Dim ftpRequest As System.Net.FtpWebRequest = System.Net.FtpWebRequest.Create("ftp://" + FTP_IP + "//" + Copy_Delete_File.filename)
- ftpRequest.Credentials = New System.Net.NetworkCredential(FTP_Account, FTP_Password) 'Set account and password
- ftpRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile 'Set method is uploadfile mathod
- System.Threading.Thread.Sleep(500)
- ftpRequest.KeepAlive = True
- ftpRequestStream = ftpRequest.GetRequestStream
- retry = False
- Catch ex As Exception
- If retrytimes <= 10 Then
- 'If retry times less than 10 do retry and add retry times, error loglist
- statusDescription = ex.Message
- retry = True
- retrytimes += 1
- 'Console.WriteLine("***Upload to FTP was Error, do retry upload, retry times : " & retrytimes)
- ErrorList.AddLog(Now.ToString("yyyyMMddHHmmss _ ") + "***Upload to FTP was Error, do retry upload, retry times : " & retrytimes)
- System.Threading.Thread.Sleep(500)
- Else
- retry = False
- retrytimes = 0
- 'Console.WriteLine("***Retry upload to FTP times was arrived")
- 'When retry arrived then add upload error filename into list and error log list
- ErrorList.AddFtp(Copy_Delete_File.filename)
- ErrorList.AddLog(Now.ToString("yyyyMMddHHmmss _ ") + "***Retry upload to FTP times was arrived")
- End If
- End Try
- End While
- 'Open copy file and then read file data
- Dim filestream As System.IO.FileStream
- Try
- filestream = System.IO.File.Open(Copy_Delete_File.copypath + "\" + Copy_Delete_File.filename, IO.FileMode.Open, IO.FileAccess.Read)
- 'filestream = System.IO.File.Open(Copy_Delete_File.sourcepath + "\" + Copy_Delete_File.filename, IO.FileMode.Open, IO.FileAccess.Read)
- Catch ex As Exception
- statusDescription = ex.Message
- End Try
- Dim buffer() As Byte = New Byte((filestream.Length) - 1) {}
- Dim byteRead As Integer
- 'Do write data into FTP file
- While True
- byteRead = filestream.Read(buffer, 0, buffer.Length)
- If byteRead = 0 Then Exit While
- ftpRequestStream.Write(buffer, 0, byteRead)
- End While
- filestream.Close()
- ftpRequestStream.Close()
- End Sub
-------------------------------------------------------------------------------------------------------------------------
今天的分享就是這樣,
有了FTP Server 的資訊之後,在我們需要的時間點啟動 FTP 上傳工作,
工作的順序大概是這樣,有上傳的位置,有帳號跟密碼,
在上傳的位置建立起我們的檔案,
再把我們要上傳的資料寫入我們剛剛建立的檔案中~
這邊居裡貓提醒大家一個小地方,因為,居裡貓也碰到了這個問題,
可能是個笨問題,但是都碰到了就順便也提醒大家,
請大家看一下程式碼中的這一段~
Dim ftpRequest As System.Net.FtpWebRequest = System.Net.FtpWebRequest.Create("ftp://" + FTP_IP + "//" + Copy_Delete_File.filename)
當時居裡貓沒有特別注意到這點,在寫這括弧中的內容就只有寫 FTP 的位置,沒有把檔名也一起寫進去,寫進去了卻沒注意到 FTP 網址需要的是 " // "反斜線啊!切忌切記~~
那就到這邊啦~其他的部分就等待下集~囉~~
謝謝大家的收看~
也感謝各位先進的教學文~
底下就是這個
1. 上報 練習 _ 選擇資料夾
2. 上報 練習 _ 寫一個 .XML 資料
3. 上報 練習 _ 監控資料夾
4. 上報 練習 _ 複製檔案
5. 上報 練習 _ FTP Server 檔案上傳
6. 上報 練習 _ 上傳錯誤紀錄
7. 上報 練習 _ 使用 BackgroundWorker 再次上傳
8. 上報 練習 _ Log 紀錄
9. 上報 練習 _ 完整程式碼分享
沒有留言:
張貼留言