如何在C#.NET/MVC4中使用ImageResizer
我在一个网站上工作,我需要裁剪和调整人们上传的图片。 我得到了上传功能的代码,但在我的httpPost操作结果中,我想重新调整图像的大小以开始。我也得到了代码,但我找不到一种方式来显示图像 这是我的密码:如何在C#.NET/MVC4中使用ImageResizer,c#,asp.net-mvc-4,imageresizer,C#,Asp.net Mvc 4,Imageresizer,我在一个网站上工作,我需要裁剪和调整人们上传的图片。 我得到了上传功能的代码,但在我的httpPost操作结果中,我想重新调整图像的大小以开始。我也得到了代码,但我找不到一种方式来显示图像 这是我的密码: [AcceptVerbs(HttpVerbs.Post)] public ActionResult FileUpload(HttpPostedFileBase uploadFile) { if (uploadFile.ContentLength > 0) {
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
if (uploadFile.ContentLength > 0)
{
foreach (string fileKey in System.Web.HttpContext.Current.Request.Files.Keys)
{
HttpPostedFile file = System.Web.HttpContext.Current.Request.Files[fileKey];
if (file.ContentLength <= 0) continue; //Skip unused file controls.
ImageResizer.ImageJob i = new ImageResizer.ImageJob(file, "~/img/", new ImageResizer.ResizeSettings("width=50;height=50;format=jpg;mode=max"));
i.CreateParentDirectory = true; //Auto-create the uploads directory.
i.Build();
string relativePath = "~/img/" + Path.GetFileName(uploadFile.FileName);
string physicalPath = Server.MapPath(relativePath);
uploadFile.SaveAs(physicalPath);
return View((object)relativePath);
}
}
[AcceptVerbs(HttpVerbs.Post)]
公共操作结果文件上载(HttpPostedFileBase上载文件)
{
如果(uploadFile.ContentLength>0)
{
foreach(System.Web.HttpContext.Current.Request.Files.Keys中的字符串fileKey)
{
HttpPostedFile=System.Web.HttpContext.Current.Request.Files[fileKey];
如果(file.ContentLength,您使用的代码允许任何人上传可执行页面并运行它
在任何情况下,都不要将uploadFile.FileName
作为最终路径名的一部分,除非您对路径和长度清理有深入的了解
其次,ImageJob需要目标路径,而不是文件夹路径。您可以使用变量,如“~/img/”
,并从i.FinalPath
访问生成的物理路径。您可以使用ImageResizer.Util.PathUtils.GuessVirtualPath(i.FinalPath)
获取应用程序的相对路径
这几乎是一个精确的副本,请考虑在发布前使用搜索功能。