Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > ASP.NET > ASP.NET Upload File > ASP.NET Restrict the types of files (extension)



Clound SSD Virtual Server

ASP.NET Restrict the types of files (extension)

ASP.NET Restrict the types of files (extension) การใช้ ASP.NET กับ FileUpload เรียกใช้ Validation controls ในการตรวจสอบนามสกุลของไฟล์ ที่สามารถ Upload ได้

Language Code : VB.NET || C#

ตัวอย่างนี้จะกำหนดเฉพาะไฟล์ mp3 และ mpeg


Code
<asp:RegularExpressionValidator
id="RegularExpressionValidator1" runat="server"
ErrorMessage="Only mp3, m3u or mpeg files are allowed!"
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))
+(.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)$"
ControlToValidate="FileUpload1"></asp:RegularExpressionValidator>


Framework : 1,2,3,4

AspNetFileUploadValidation.aspx

<html>
<head>
<title>ThaiCreate.Com ASP.NET - Upload files</title>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
		<asp:FileUpload ID="FileUpload1" runat="server" /><br />
		<br />
		<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" 
		 Text="Upload File" />&nbsp;<br />
		<br />
		<asp:Label ID="Label1" runat="server"></asp:Label>
		<asp:RegularExpressionValidator 
		 id="RegularExpressionValidator1" runat="server" 
		 ErrorMessage="Only mp3, m3u or mpeg files are allowed!" 
		 ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))
		    +(.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)$" 
		 ControlToValidate="FileUpload1"></asp:RegularExpressionValidator>
		<br />
		<asp:RequiredFieldValidator 
		 id="RequiredFieldValidator1" runat="server" 
		 ErrorMessage="This is a required field!" 
		 ControlToValidate="FileUpload1"></asp:RequiredFieldValidator>
    </form>
</body>
</html>


หรือจะทำที่ฝั่ง Server ก็ได้


VB.NET

    Protected Sub Button1_Click(ByVal sender As Object, _
      ByVal e As System.EventArgs)
        If FileUpload1.HasFile Then
            Dim fileExt As String
            fileExt = System.IO.Path.GetExtension(FileUpload1.FileName)
            
            If (fileExt = ".mp3") Then
                Try
                    FileUpload1.SaveAs("C:\Uploads\" & _
                       FileUpload1.FileName)
                    Label1.Text = "File name: " & _
                      FileUpload1.PostedFile.FileName & "<br>" & _
                      "File Size: " & _
                      FileUpload1.PostedFile.ContentLength & " kb<br>" & _
                      "Content type: " & _
                      FileUpload1.PostedFile.ContentType
                Catch ex As Exception
                    Label1.Text = "ERROR: " & ex.Message.ToString()
                End Try
            Else
                Label1.Text = "Only .mp3 files allowed!"
            End If
        Else
            Label1.Text = "You have not specified a file."
        End If
    End Sub



Screenshot

ASP.NET Restrict the types of files (extension)






   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2011-06-16 06:30:08 / 2011-06-16 06:41:18
  Download : No files
 Sponsored Links / Related

 
ASP.NET Upload file (HTMLInputFile)
Rating :

 
ASP.NET Upload file (HTMLInputFile - runat="server")
Rating :

 
ASP.NET Upload file (HTMLInputFile - Multiple upload) P.1
Rating :

 
ASP.NET Upload file (HTMLInputFile - Multiple upload) P.2
Rating :

 
ASP.NET Upload file (FileUpload Control)
Rating :

 
ASP.NET Upload & Multiple-FindControl
Rating :

 
ASP.NET Upload & Create Control - FindControl
Rating :

 
ASP.NET Upload file to Database
Rating :

 
ASP.NET Upload file & BLOB Image Binary
Rating :

 
ASP.NET Limit Max Size Upload file
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่