【ASP.NET】【VB】ファイルアップロードを行う(File up load)

コントロール構成

 * Label : 1
 * RadioButtonList : 1 (上書き許容・禁止を指定。プロパティ「Items」に、「permit」および「forbid」を指定)
 * FileUpload : 1
 * Button : 1

サンプル

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim postFile As HttpPostedFile = Me.FileUpload1.PostedFile
    Dim upPath As String = Server.MapPath("~/App_Data/" & Path.GetFileName(postFile.FileName))

    If RadioButtonList1.SelectedValue = "forbid" And File.Exists(upPath) Then
        Me.Label1.Text = "There is the same file name in the folder."
    Else
        If postFile.ContentType = "image/jpeg" Then
            postFile.SaveAs(upPath)
            Me.Label1.Text = postFile.FileName & " is uploaded."
        Else
            Me.Label1.Text = "Sorry... This system can upload only Jpeg images."
        End If
    End If

End Sub