[UserControl] 一個可以檢查伺服器端是否已有同名檔案的圖檔專用 FileUpload 控制項

我把這個功能寫成使用者控制項,其功能如下:1. 上傳檔案(基本功能), 2. 可以檢查網站是否已有同名的檔案存在, 3. 若有同名檔案存在,可選擇是否予以覆蓋, 4. 可以指定上傳位址...

 

我把這個功能寫成使用者控制項,其功能如下:

  1. 上傳檔案(基本功能)
  2. 可以檢查網站是否已有同名的檔案存在
  3. 若有同名檔案存在,可選擇是否予以覆蓋
  4. 可以指定上傳位址

使用者控制項的程式如下:

ucImageUpload.ascx -

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ucImageUpload.ascx.vb" Inherits="UserControls_ucImageUpload" %>
上傳圖檔:<br />
<asp:FileUpload ID="FileUpload1" runat="server" Font-Names="Arial" Font-Size="Small" Width="400px" /><br />
<asp:Button ID="Button1" runat="server" Text="上傳" />
<asp:CheckBox ID="cbOverwrite" runat="server" Text="覆寫舊檔" /><br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>

ucImageUpload.ascx.vb -

Partial Class UserControls_ucImageUpload
    Inherits System.Web.UI.UserControl

    Private _UploadPath As String = "~/images/"

    Public Property UploadPath() As String
        Get
            Return _UploadPath
        End Get
        Set(ByVal value As String)
            _UploadPath = value
        End Set
    End Property

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If FileUpload1.HasFile Then
            Try
                Dim ValidUpload As Boolean
                If FileExists(FileUpload1.FileName) Then '資料夾中已有同名檔案存在
                    Trace.Write("Click()", "fileName: " & FileUpload1.FileName & "檔案已存在!")
                    If cbOverwrite.Checked Then '資料夾中已有同名檔案存在且有勾選「覆寫舊檔」
                        ValidUpload = True
                    Else '資料夾中已有同名檔案存在但未勾選「覆寫舊檔」
                        ValidUpload = False
                        Label1.Text = "錯誤: 網站中已有同名檔案存在。如果你確定要覆蓋該檔案,你必須勾選「覆寫舊檔」。"
                    End If
                Else '資料夾中並無同名檔案存在
                    ValidUpload = True
                End If
                If ValidUpload Then
                    If FileUpload1.PostedFile.ContentType = "image/pjpeg" Or _
                          FileUpload1.PostedFile.ContentType = "image/gif" Or _
                          FileUpload1.PostedFile.ContentType = "image/x-png" Then '檢查是否為可接受的圖檔
                        FileUpload1.SaveAs(Server.MapPath(UploadPath & FileUpload1.FileName))
                        Label1.Text = "檔案上傳成功!<br />" & _
                            "&nbsp;&nbsp; >  檔名: " & FileUpload1.PostedFile.FileName & "<br>" & _
                            "&nbsp;&nbsp; >  大小: " & FileUpload1.PostedFile.ContentLength & " kb<br>" & _
                            "&nbsp;&nbsp; >  類型: " & FileUpload1.PostedFile.ContentType
                    Else '不是圖檔,不允許上傳
                        Label1.Text = "抱歉,您只能上傳圖形格式檔案!您所上傳的檔案不在接受範圍內。"
                    End If
                End If
            Catch ex As Exception
                Label1.Text = "錯誤: 發生不明錯誤,請通報網站管理員。可能因為檔案太大或傳輸問題所引起。"
                Trace.Warn("ucImageUpload.Button1_Click()", ex.Message.ToString())
            End Try
        Else '使用者未選取檔案或檔案在客戶端找不到
            Label1.Text = "您必須選擇一個檔案才能上傳"
        End If
    End Sub

    Protected Function FileExists(ByVal fileName As String) As Boolean
        Return My.Computer.FileSystem.FileExists(Server.MapPath(UploadPath & fileName))
    End Function

End Class

 

 


Dev 2Share @ 點部落