C#newbie问题第2部分 - 声明类和方法

时间:2010-03-26 16:06:11

标签: c# asp.net-mvc class methods declaration

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload(Photo photo)
{
    foreach (string file in Request.Files)
    {

        var path = "~/Uploads/Photos/";

        HttpPostedFileBase posted = (HttpPostedFileBase)Request.Files[file];

        if (posted.ContentLength > 0)
        {
            photo.Date = DateTime.Now;
            photo.AlbumID = 1;
            photo.Title = Path.GetFileName(posted.FileName);

            photoRepository.Add(photo);
            photoRepository.Save();

            posted.SaveAs(Server.MapPath(path + photo.PhotoID + ".jpg"));

            Image img1 = Image.FromFile(Server.MapPath("~/Uploads/Photos/") + photo.PhotoID + ".jpg");

            imageHelper.CreateThumbnail(posted, img1);

            int newWidth = 100;
            int newHeight = 100;
            double ratio = 0;

            if (img1.Width > img1.Height)
            {
                ratio = img1.Width / (double)img1.Height;
                newHeight = (int)(newHeight / ratio);
            }
            else
            {
                ratio = img1.Height / (double)img1.Width;
                newWidth = (int)(newWidth / ratio);
            }

            Image bmp1 = img1.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
            bmp1.Save(Server.MapPath("~/Uploads/Photos/Thumbnails/") + photo.PhotoID + ".jpg");

            img1.Dispose();
            bmp1.Dispose();
        }
    }

    return RedirectToAction("Index");
}

我想更好地组织这段代码,如下所示:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload(Photo photo)
{
    foreach (string file in Request.Files)
    {

        var path = "~/Uploads/Photos/";

        HttpPostedFileBase posted = (HttpPostedFileBase)Request.Files[file];
        ImageHelper imageHelper = new ImageHelper();  

        if (posted.ContentLength > 0)
        {
            photo.Date = DateTime.Now;
            photo.AlbumID = 1;
            photo.Title = Path.GetFileName(posted.FileName);

            photoRepository.Add(photo);
            photoRepository.Save();

            posted.SaveAs(Server.MapPath(path + photo.PhotoID + ".jpg"));

            Image img1 = Image.FromFile(Server.MapPath("~/Uploads/Photos/") + photo.PhotoID + ".jpg");

            // Create thumbnail
            imageHelper.CreateThumbnail(posted, img1);                    
        }
    }

    return RedirectToAction("Index");
}

在这里,在Helpers文件夹中我创建了处理缩略图的类和方法:

public class ImageHelper
{        
    public void CreateThumbnail(HttpPostedFileBase posted, Image img1)
    {
        int newWidth = 100;
        int newHeight = 100;
        double ratio = 0;

        if (img1.Width > img1.Height)
        {
            ratio = img1.Width / (double)img1.Height;
            newHeight = (int)(newHeight / ratio);
        }
        else
        {
            ratio = img1.Height / (double)img1.Width;
            newWidth = (int)(newWidth / ratio);
        }

        Image bmp1 = img1.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
        bmp1.Save(Server.MapPath("~/Uploads/Photos/Thumbnails/") + photo.PhotoID + ".jpg");

        img1.Dispose();
        bmp1.Dispose();
    } 
}

但是我得到编译错误,Server(在bmp1.Save(Server.MapPath ...中的ImageHelper类中)在当前上下文中不存在,而如果代码在一个地方它可以正常工作。
我做错了什么,这是宣告方法和组织代码的正确方法吗?

提前致谢,
ILE

2 个答案:

答案 0 :(得分:2)

ServerController类的属性。这就是Update行动中可以访问的原因。为了从外部使用它,您可以通过HttpContext.Current.Server获得它。

但是,这将使您的代码不易测试,并且与ASP.NET细节更紧密地结合在一起。相反,重写此方法,使其独立于任何与ASP.NET相关的东西。

在这种特殊情况下,您可以向方法添加其他参数,这将告诉它保存缩略图的位置:

void CreateThumbnail(Image img1, string targetDirectory)

答案 1 :(得分:2)

您应该将这些资源传递给您的函数,而不是让您的CreateThumbnail方法直接调用“服务器”。这样可以更好地分离关注点,最终实现更好的可重用性。此外,它还可以解决您的问题。