Asp. NET how to determine if a file in a directory is a picture

Source: Internet
Author: User

The Uploadfiles directory for ASP.net MVC applications is to store files uploaded by the user, including, of course, pictures. Now the requirement is to display all users uploading pictures as a list and presenting them to the user.

If you go directly to the display, maybe some files are not picture files. It is also possible that the extension was changed when the user uploaded. True is not a picture, changed to a picture-related extension. That is to say, now to do is to filter out the real picture file out.

In the application's utilities directory, create a imageutility category:

There are two functions, the first public static string base64string (String virtualpath):

There is a parameter that needs to be passed in to the virtual path.

#13使用Server. MapPath () processes the virtual path as a physical path.

#14读取文件流 (FileStream)

#15把文件流处理为byte阵列.

#17转换为Convert. ToBase64String () string.

The second function is public static bool Isimage (string base64string):

#22把参数传入的Base64String字符串, converted to a byte array.

#24行, convert byte[] to memory stream (MemoryStream)

#28行是核心判断是否图片代码.

Two functions, once each line is interpreted, can be integrated into a function:

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

The consolidated function, renamed IsImage1 (), is an optimized function that is less than two byte array conversions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.