繁体   English   中英

C#新手问题第二部分-声明类和方法

[英]C# newbie problem part 2 - declaring class and method

[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 bmp1.Save(Server.MapPath ...行中)的ImageHelper类中)在当前上下文中不存在,但是如果代码放在一个地方,它可以很好地工作。
我在做什么错,这甚至是声明方法和组织代码的正确方法吗?

提前致谢,

ServerController类的属性。 这就是为什么可以在“ Update操作中访问它的原因。 为了从外部使用它,您可以通过HttpContext.Current.Server获取它。

但是,这将使您的代码可测试性降低,并与ASP.NET特定要求更加紧密地结合在一起。 而是重写此方法,以使其独立于任何与ASP.NET相关的内容。

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

void CreateThumbnail(Image img1, string targetDirectory)

与其让CreateThumbnail方法直接从“服务器”中调用事物,不如将这些资源传递给函数。 这样可以更好地分离问题,并最终实现更好的可重用性。 此外,它也可以解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM