开发者

pictures validation in MVC

Goal:

Make a evaluation of the picture's format, width and height and then saving it in my program.

problem:

Don't know how to use the HttpPostedFileBase file and then sending it to Image newImage = Image.FromFile(xxxx); without saving the picture in my program.

  1. Validation
  2. save picture in my "App_Data"
[AcceptVerbs(HttpVerbs.Post)]  
public ActionResult Add(HttpPostedFileBase file)  
{
    if (file.ContentLength > 0)
    {
        Image newImage = Image.FromFile(xxxx);      
    }

    return Index();  
 开发者_JAVA技巧} 


You could do this somehow like the following snippet. Notice the System.Drawing namespace reference, you will need for the Image.FromStream() method.

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


HttpPostedFile has a stream property that is the uploaded data. Use that as with the Image.FromStream method to load the image.

I'd suggest you read the help about HttpPostedFile here:

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

Simon

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜