栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

确定上传的文件在MVC上是否为图像(任何格式)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

确定上传的文件在MVC上是否为图像(任何格式)

如果它可以帮助任何人,这是HttpPostedFilebase的静态方法,该方法检查给定的上传文件是否为图像:

public static class HttpPostedFilebaseExtensions{    public const int ImageMinimumBytes = 512;    public static bool IsImage(this HttpPostedFilebase postedFile)    {        //-------------------------------------------        //  Check the image mime types        //-------------------------------------------        if (!string.Equals(postedFile.ContentType, "image/jpg", StringComparison.OrdinalIgnoreCase) && !string.Equals(postedFile.ContentType, "image/jpeg", StringComparison.OrdinalIgnoreCase) && !string.Equals(postedFile.ContentType, "image/pjpeg", StringComparison.OrdinalIgnoreCase) && !string.Equals(postedFile.ContentType, "image/gif", StringComparison.OrdinalIgnoreCase) && !string.Equals(postedFile.ContentType, "image/x-png", StringComparison.OrdinalIgnoreCase) && !string.Equals(postedFile.ContentType, "image/png", StringComparison.OrdinalIgnoreCase))        { return false;        }        //-------------------------------------------        //  Check the image extension        //-------------------------------------------        var postedFileExtension = Path.GetExtension(postedFile.FileName);        if (!string.Equals(postedFileExtension , ".jpg", StringComparison.OrdinalIgnoreCase) && !string.Equals(postedFileExtension , ".png", StringComparison.OrdinalIgnoreCase) && !string.Equals(postedFileExtension , ".gif", StringComparison.OrdinalIgnoreCase) && !string.Equals(postedFileExtension , ".jpeg", StringComparison.OrdinalIgnoreCase))        { return false;        }        //-------------------------------------------        //  Attempt to read the file and check the first bytes        //-------------------------------------------        try        { if (!postedFile.InputStream.CanRead) {     return false; } //------------------------------------------ //   Check whether the image size exceeding the limit or not //------------------------------------------  if (postedFile.ContentLength < ImageMinimumBytes) {     return false; } byte[] buffer = new byte[ImageMinimumBytes]; postedFile.InputStream.Read(buffer, 0, ImageMinimumBytes); string content = System.Text.Encoding.UTF8.GetString(buffer); if (Regex.IsMatch(content, @"<script|<html|<head|<title|<body|<pre|<table|<as+href|<img|<plaintext|<cross-domain-policy",     RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline)) {     return false; }        }        catch (Exception)        { return false;        }        //-------------------------------------------        //  Try to instantiate new Bitmap, if .NET will throw exception        //  we can assume that it's not a valid image        //-------------------------------------------        try        { using (var bitmap = new System.Drawing.Bitmap(postedFile.InputStream)) { }        }        catch (Exception)        { return false;        }        finally        {  postedFile.InputStream.Position = 0;        }        return true;    }}

编辑2/10/2017:根据建议的编辑,添加了finally语句以重置流,因此我们以后可以使用它。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/415314.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号