


Three ways to determine the type of uploaded files in asp.net
1. The security is relatively low. Changing the text file 1.txt to 1.jpg can still be uploaded, but the implementation method is easy to understand and simple to implement, so many people on the Internet still use this method.
Boolean fileOk = false; string path = Server.MapPath("~/images/"); //判断是否已经选取文件 if (FileUpload1.HasFile) { //取得文件的扩展名,并转换成小写 string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower(); //限定只能上传jpg和gif图片 string[] allowExtension = { ".jpg", ".gif" }; //对上传的文件的类型进行一个个匹对 int j = 0; for (int i = 0; i < allowExtension.Length; i++) { if (fileExtension == allowExtension[i]) { fileOk = true; return; } else { j++; } } if (j > 0) { Response.Write("<script>alert('文件格式不正确');</script>"); return; } } else { Response.Write("<script>alert('你还没有选择文件');</script>"); return; } //如果扩展名符合条件,则上传 if (fileOk) { FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName); Response.Write("<script>alert('上传成功');</script>"); }
Second, do not detect the file suffix but detect the file MIME content type.
Boolean fileOk = false; string path = Server.MapPath("~/images/"); //判断是否已经选取文件 if (FileUpload1.HasFile) { //取得文件MIME内容类型 string type = this.FileUpload1.PostedFile.ContentType.ToLower(); if (type.Contains("image")) //图片的MIME类型为"image/xxx",这里只判断是否图片。 { fileOk = true; } else { Response.Write("<script>alert('格式不正确')</script>"); } } else { Response.Write("<script>alert('你还没有选择文件');</script>"); } //如果扩展名符合条件,则上传 if (fileOk) { FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName); Response.Write("<script>alert('上传成功');</script>"); }
3. Can realize file type judgment in the true sense
try { //判断是否已经选取文件 if (FileUpload1.HasFile) { if (IsAllowedExtension(FileUpload1)) { string path = Server.MapPath("~/images/"); FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName); Response.Write("<script>alert('上传成功');</script>"); } else { Response.Write("<script>alert('您只能上传jpg或者gif图片');</script>"); } } else { Response.Write("<script>alert('你还没有选择文件');</script>"); } } catch (Exception error) { Response.Write(error.ToString()); } #endregion } //真正判断文件类型的关键函数 public static bool IsAllowedExtension(FileUpload hifile) { System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.BinaryReader r = new System.IO.BinaryReader(fs); string fileclass = ""; //这里的位长要具体判断. byte buffer; try { buffer = r.ReadByte(); fileclass = buffer.ToString(); buffer = r.ReadByte(); fileclass += buffer.ToString(); } catch { } r.Close(); fs.Close(); if (fileclass == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar { return true; } else { return false; } }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.
