『ASP.NET』同時多檔案上傳 – 使用 FileUpload 元件
- 當檔案重複時,新增檔案序號的方式與位置
- 使用 Try … Catch
- 註解說明
程式碼!
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)Handles Button1.Click
Jupload( “J") ‘當中的J是用來做檔案序號前的附加代號 Prefix
End Sub
Jupload( “J") ‘當中的J是用來做檔案序號前的附加代號 Prefix
End Sub
Function Jupload(ByVal prefix As String) As Boolean Dim upload_path As String = “\Uploads\" ‘要放上傳檔案的路徑必須要先建立 (在檔案總管理新增資料夾)
Dim htdocs As String = Request.PhysicalApplicationPath ‘抓取目前網頁在實體資料夾路徑位置
Dim i As Integer Dim check As Integer = 0
For i = 1 To (Request.Files.Count) ‘FileUpload 數量的迴圈
Dim fileuploadx As New FileUpload ‘定義一個 FileUpload 物件來對應 aspx 所拉入的 FileUpload 元件
‘—–註:aspx 所拉入的 FileUpload 元件若名稱有更改,請連動更改下面的對應方式
fileuploadx = CType(Page.FindControl(“FileUpload" & i), FileUpload) ‘ id 對應
If (fileuploadx.HasFile) Then ‘FileUploads 元件有選檔案才做
Dim fileName As String = fileuploadx.FileName ‘Fileupload 元件所拉入的檔案名稱
Dim filename_part() As String = fileName.Split(“.")
Dim fileNamex As String = “" Dim pathToCheck As String = htdocs & upload_path & fileName ‘實體路徑+上傳目錄+檔名
Dim check_repeat As Integer = 0
If (System.IO.File.Exists(pathToCheck)) Then ‘判斷檔案是否存在
check_repeat = 2 ‘設定初始 prefix=2,當檔案名稱重複的時候
While (System.IO.File.Exists(pathToCheck))
fileNamex = “" If filename_part.GetLength(0) = 1 Then fileNamex = fileName
Else Dim j As Integer For j = 0 To filename_part.GetLength(0) – 2
fileNamex &= filename_part(j)
If j <> filename_part.GetLength(0) – 2 Then fileNamex &= “." Next fileNamex &= “_" & prefix & String.Format(“{0:000}", check_repeat) & “."& filename_part(filename_part.GetLength(0) – 1)
End If pathToCheck = htdocs & upload_path & fileNamex ‘重新串一次完整路徑,以再做檢查重複
check_repeat = check_repeat + 1
End While End If Try fileuploadx.SaveAs(pathToCheck) ‘真正做上傳的動作
If check_repeat = 0 Then lbl_hint1.Text &= fileName & “上傳成功
" Else lbl_hint1.Text &= fileName & “上傳成功,並且更名為:“ & fileNamex &“
" End If Catch ex As Exception
lbl_hint1.Text &= “「上傳失敗」“ & ex.Message & “
" check += 1
End Try End If ‘判斷是否有重複
Next ‘FileUpload 數量的迴圈
If check = 0 Then Return True Else Return False End If
End Function
Dim htdocs As String = Request.PhysicalApplicationPath ‘抓取目前網頁在實體資料夾路徑位置
Dim i As Integer Dim check As Integer = 0
For i = 1 To (Request.Files.Count) ‘FileUpload 數量的迴圈
Dim fileuploadx As New FileUpload ‘定義一個 FileUpload 物件來對應 aspx 所拉入的 FileUpload 元件
‘—–註:aspx 所拉入的 FileUpload 元件若名稱有更改,請連動更改下面的對應方式
fileuploadx = CType(Page.FindControl(“FileUpload" & i), FileUpload) ‘ id 對應
If (fileuploadx.HasFile) Then ‘FileUploads 元件有選檔案才做
Dim fileName As String = fileuploadx.FileName ‘Fileupload 元件所拉入的檔案名稱
Dim filename_part() As String = fileName.Split(“.")
Dim fileNamex As String = “" Dim pathToCheck As String = htdocs & upload_path & fileName ‘實體路徑+上傳目錄+檔名
Dim check_repeat As Integer = 0
If (System.IO.File.Exists(pathToCheck)) Then ‘判斷檔案是否存在
check_repeat = 2 ‘設定初始 prefix=2,當檔案名稱重複的時候
While (System.IO.File.Exists(pathToCheck))
fileNamex = “" If filename_part.GetLength(0) = 1 Then fileNamex = fileName
Else Dim j As Integer For j = 0 To filename_part.GetLength(0) – 2
fileNamex &= filename_part(j)
If j <> filename_part.GetLength(0) – 2 Then fileNamex &= “." Next fileNamex &= “_" & prefix & String.Format(“{0:000}", check_repeat) & “."& filename_part(filename_part.GetLength(0) – 1)
End If pathToCheck = htdocs & upload_path & fileNamex ‘重新串一次完整路徑,以再做檢查重複
check_repeat = check_repeat + 1
End While End If Try fileuploadx.SaveAs(pathToCheck) ‘真正做上傳的動作
If check_repeat = 0 Then lbl_hint1.Text &= fileName & “上傳成功
" Else lbl_hint1.Text &= fileName & “上傳成功,並且更名為:“ & fileNamex &“
" End If Catch ex As Exception
lbl_hint1.Text &= “「上傳失敗」“ & ex.Message & “
" check += 1
End Try End If ‘判斷是否有重複
Next ‘FileUpload 數量的迴圈
If check = 0 Then Return True Else Return False End If
End Function
Reference:
- http://www.dotblogs.com.tw/mis2000lab/archive/2008/05/14/3986.aspx
- http://blog.miniasp.com/?tag=/fileupload
——————————
關鍵摘要
- Request.PhysicalApplicationPath ‘抓取目前網頁在實體資料夾路徑位置
- Request.Files.Count ‘取得前端 FileUpload 數量
- CType(Page.FindControl(“FileUpload" & i), FileUpload) ‘ 物件id 對應
- Dim filename_part() As String = fileName.Split(“.") ‘ 檔名、字串分割,並存入 String Array
- filename_part.GetLength(0) ‘ 抓取依符號分割後的子字串數量,0是指維度
- System.IO.File.Exists(pathToCheck) ‘ 檢查檔案是否已經存在
- fileuploadx.SaveAs(pathToCheck) ‘檔案上傳的動作
沒有留言:
張貼留言