محدود کردن کاربرها به آپلود فایلهایی خاص در ASP.NET MVC
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۰۴/۱۶ ۰:۱
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
namespace SecurityModule
{
public class AllowUploadSpecialFilesOnlyAttribute : ActionFilterAttribute
{
readonly List<string> _toFilter = new List<string>();
readonly string _extensionsWhiteList;
public AllowUploadSpecialFilesOnlyAttribute(string extensionsWhiteList)
{
if (string.IsNullOrWhiteSpace(extensionsWhiteList))
throw new ArgumentNullException("extensionsWhiteList");
_extensionsWhiteList = extensionsWhiteList;
var extensions = extensionsWhiteList.Split(',');
foreach (var ext in extensions.Where(ext => !string.IsNullOrWhiteSpace(ext)))
{
_toFilter.Add(ext.ToLowerInvariant().Trim());
}
}
bool canUpload(string fileName)
{
if (string.IsNullOrWhiteSpace(fileName)) return false;
var ext = Path.GetExtension(fileName.ToLowerInvariant());
return _toFilter.Contains(ext);
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var files = filterContext.HttpContext.Request.Files;
foreach (string file in files)
{
var postedFile = files[file];
if (postedFile == null || postedFile.ContentLength == 0) continue;
if (!canUpload(postedFile.FileName))
throw new InvalidOperationException(
string.Format("You are not allowed to upload {0} file. Please upload only these files: {1}.",
Path.GetFileName(postedFile.FileName),
_extensionsWhiteList));
}
base.OnActionExecuting(filterContext);
}
}
}
[AllowUploadSpecialFilesOnly(".jpg,.gif,.png")]
public ActionResult ImageUpload(HttpPostedFileBase file)
public static bool IsImageFile(HttpPostedFileBase photoFile)
{
using (var img = Image.FromStream(photoFile.InputStream))
{
return img.Width > 0;
}
}
public class CustomFilter : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
<location path="upload">
<system.webServer>
<handlers accessPolicy="Read" />
</system.webServer>
</location>