這次居裡貓要把所有上傳失敗的資訊做個紀錄!
以便另一次的重新上傳使用~所以這集跟下集算是關係良好的....嗯!
第六集講講當我們之前寫得重新上傳機制依然沒有辦法上傳成功該怎麼辦呢!
大家應該都有發現居裡貓在程式上傳的地方都有重新上傳機制,
而且設定都只有10次,當然啦,就程式而以一下就滿足條件了~
那這一點點時間內還是沒辦法重新上傳成功該怎麼辦!?
總不能放著就不上傳吧~
那我們要再次重新上傳又不能影響到新的檔案寫入時的上傳~
首先我們要有個東西可以記錄我的上傳失敗的資訊,
再來才使想想怎麼把這些資訊再次上傳!
所以這次居裡貓就使用了 List 的方法來把上傳失敗的資訊記錄起來~
以便之後我們再次上傳使用~~~
好囉!那就看看程式碼吧~
-----------------------------------------------------------------------------------------------------------------------
- '這次反向操作,先上大家看一下存上傳失敗資訊的 List 的 Class 建立起來長什麼鳥樣子
- '這裡先提醒大家,大家會看到三個 List 的名字,那也是分別他們的功能
- '現在我們提到的都是上傳失敗的問題,所以是 NAS and FTP
- '至於 Log 那是之後會說明的部分,大家也可以先看~
- 'About error list, NAS , FTP, log
- Public Class ErrorList
- Public Shared NASList As New List(Of String)
- Public Shared FTPList As New List(Of String)
- Public Shared LogList As New List(Of String)
- Public Shared Sub AddNas(ByVal nList As String)
- NASList.Add(nList)
- End Sub
- Public Shared Sub AddFtp(ByVal fList As String)
- FTPList.Add(fList)
- End Sub
- Public Shared Sub AddLog(ByVal log As String)
- LogList.Add(log)
- End Sub
- Public Shared Sub ClearNas()
- NASList.Clear()
- End Sub
- Public Shared Sub ClearFtp()
- FTPList.Clear()
- End Sub
- Public Shared Sub ClaerLog()
- LogList.Clear()
- End Sub
- Public Shared Function CountNas()
- Return NASList.Count
- End Function
- Public Shared Function CountFtp()
- Return FTPList.Count
- End Function
- Public Shared Sub RemoveNAS(ByVal i As Integer)
- Dim str As String = NASList(i)
- NASList.Remove(str)
- End Sub
- Public Shared Sub RemoveFTP(ByVal i As Integer)
- Dim str As String = FTPList(i)
- FTPList.Remove(str)
- End Sub
- End Class
- '------分割線,下面是在複製檔案時使用到紀錄上傳失敗的部分
- '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)
- 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 Server 上傳失敗的紀錄
- '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
- '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
- 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
---------------------------------------------------------------------------------------------------------------------
今天的分享就是個紀錄一些資訊,之後我們要利用這個資訊來做其他的重新上傳。
仔細看下來記錄這些資訊的地方就是當我們上傳失敗的時候嘛!
這個是很直覺的~~
如何利用這些資訊,再次的重新上傳方法請待下集。
感謝各位收看,感謝各位先進的教學文。
底下就是這個
1. 上報 練習 _ 選擇資料夾
2. 上報 練習 _ 寫一個 .XML 資料
3. 上報 練習 _ 監控資料夾
4. 上報 練習 _ 複製檔案
5. 上報 練習 _ FTP Server 檔案上傳
6. 上報 練習 _ 上傳錯誤紀錄
7. 上報 練習 _ 使用 BackgroundWorker 再次上傳
8. 上報 練習 _ Log 紀錄
9. 上報 練習 _ 完整程式碼分享
沒有留言:
張貼留言