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.