1

I'm working on a site where I need to crop and resize images that people upload. I got the code for the upload function but in my httpPost action result I want to resize the image to start off with. I also got the code for that but I can't find a way to show the image.

Here is my code:

[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);

    }
}

I want go write out the Image information from ImageResizer.ImageJob i.. Any ideas? Thank you!

4

1 回答 1

2

首先,您使用的代码允许任何人上传可执行页面并运行它

在任何情况下,都不应将uploadFile.FileName其用作最终路径名的一部分,除非您深入了解路径和长度清理。

其次,ImageJob 需要一个目标路径,而不是文件夹路径。您可以使用变量,例如"~/img/<guid>.<ext>"并从i.FinalPath. 您可以使用ImageResizer.Util.PathUtils.GuessVirtualPath(i.FinalPath)来获取应用程序相对路径。

由于这几乎是mvc3 ImageResizer的精确副本,因此请在发布前考虑使用搜索功能。

于 2013-11-07T19:45:07.793 回答