【发布时间】:2016-01-29 14:10:14
【问题描述】:
我在这里找到了一个很好的方法来检查用户上传的文件是否是图片,但我在尝试实现它时遇到了问题。
这是我找到的检查文件的类
public static class HttpPostedFileBaseExtensions
{
public const int ImageMinimumBytes = 512;
public static bool IsImage(this HttpPostedFileBase postedFile)
{
//-------------------------------------------
// Check the image mime types
//-------------------------------------------
if (postedFile.ContentType.ToLower() != "image/jpg" &&
postedFile.ContentType.ToLower() != "image/jpeg" &&
postedFile.ContentType.ToLower() != "image/pjpeg" &&
postedFile.ContentType.ToLower() != "image/gif" &&
postedFile.ContentType.ToLower() != "image/x-png" &&
postedFile.ContentType.ToLower() != "image/png")
{
return false;
}
//-------------------------------------------
// Check the image extension
//-------------------------------------------
if (Path.GetExtension(postedFile.FileName).ToLower() != ".jpg"
&& Path.GetExtension(postedFile.FileName).ToLower() != ".png"
&& Path.GetExtension(postedFile.FileName).ToLower() != ".gif"
&& Path.GetExtension(postedFile.FileName).ToLower() != ".jpeg")
{
return false;
}
//-------------------------------------------
// Attempt to read the file and check the first bytes
//-------------------------------------------
try
{
if (!postedFile.InputStream.CanRead)
{
return false;
}
if (postedFile.ContentLength < ImageMinimumBytes)
{
return false;
}
byte[] buffer = new byte[512];
postedFile.InputStream.Read(buffer, 0, 512);
string content = System.Text.Encoding.UTF8.GetString(buffer);
if (Regex.IsMatch(content, @"<script|<html|<head|<title|<body|<pre|<table|<a\s+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;
}
return true;
}
}
我的个人资料课程
public class Profile
{
public int ProfileID { get; set; }
[Required(ErrorMessage = "Please enter a profile name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter a intro")]
public string Intro { get; set; }
[Required(ErrorMessage = "Please enter a description")]
public string Description { get; set; }
public decimal Rate { get; set; }
public byte[] ImageData { get; set; }
public string ImageMimeType { get; set; }
}
我的 ProfileController 更改后。我添加了 HttpPostedFileBase 作为参数,还使用了这一行 if (HttpPostedFileBaseExtensions.IsImage(file) == true) ,我认为这可以解决问题,但没有任何区别。
[HttpPost]
public ActionResult Edit(Profile profile, HttpPostedFileBase file)
{
if (HttpPostedFileBaseExtensions.IsImage(file) == true)
{
if (ModelState.IsValid)
{
repository.SaveProfile(profile);
TempData["message"] = string.Format("{0} has been saved", profile.Name);
return RedirectToAction("List");
}
else
{
// there is something wrong with the data values
return View(profile);
}
}
else
{
return View(ViewBag);
}
}
最后是来自存储库的 SaveProfile 方法。
public void SaveProfile(Profile profile)
{
Profile dbEntry = context.Profiles.Find(profile.ProfileID);
if (profile.ProfileID == 0)
{
context.Profiles.Add(profile);
}
else
{
if (dbEntry != null)
{
dbEntry.Name = profile.Name;
dbEntry.Rate = profile.Rate;
dbEntry.Intro = profile.Intro;
dbEntry.Description = profile.Description;
if (profile.ImageData != null)
{
dbEntry.ImageData = profile.ImageData;
dbEntry.ImageMimeType = profile.ImageMimeType;
}
}
}
context.SaveChanges();
}
我还尝试编辑SaveProfile 方法,但无法实现类中的所有功能,我宁愿将其分开并按原样使用。任何想法我哪里出错了?
【问题讨论】:
-
在调试器中检查
-
我尝试过控制台调试或什么:),但并没有真正了解它。 Webapp 可以正常工作!当我上传文件时,它会保存所有文件,但它适用于任何文件。如果我上传一个exe文件,它仍然有效。在数据库中,我可以看到 mime 与图片无关,因此在我的“ProfileController”中,第一行不做任何事情。它应该将文件与配置文件实例分开并将其发送以检查它,如果它返回 true,则应该保存它。
标签: asp.net-mvc entity-framework asp.net-mvc-5