【问题标题】:pictures validation in MVCMVC中的图片验证
【发布时间】:2011-02-03 15:02:35
【问题描述】:

目标:
对图片的格式、宽度和高度进行评估,然后将其保存到我的程序中。

问题:
不知道怎么用HttpPostedFileBase file然后发给Image newImage = Image.FromFile(xxxx);而不把图片保存在我的程序里。

  1. 验证
  2. 将图片保存在我的“App_Data”中
[AcceptVerbs(HttpVerbs.Post)]  
public ActionResult Add(HttpPostedFileBase file)  
{
    if (file.ContentLength > 0)
    {
        Image newImage = Image.FromFile(xxxx);      
    }

    return Index();  
 } 

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-2


    【解决方案1】:

    你可以像下面的 sn-p 那样做。注意 System.Drawing 命名空间引用,您将需要 Image.FromStream() 方法。

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Add(HttpPostedFileBase httpPostedFileBase)
    {
        using (System.Drawing.Image image = System.Drawing.Image.FromStream(httpPostedFileBase.InputStream, true, true))
        {
            if (image.Width == 100 && image.Height == 100)
            {
                var file = @"D:\test.jpg";
                image.Save(file);
            }
        }
    
        return View();
    }
    

    【讨论】:

      【解决方案2】:

      HttpPostedFile 有一个流属性,即上传的数据。将其与 Image.FromStream 方法一起使用来加载图像。

      我建议您在此处阅读有关 HttpPostedFile 的帮助:

      http://msdn.microsoft.com/en-us/library/SYSTEM.WEB.HTTPPOSTEDFILE(v=vs.100,d=lightweight).aspx

      西蒙

      【讨论】:

        猜你喜欢
        • 2011-11-25
        • 2014-06-16
        • 2017-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多