简体   繁体   English

在 asp.net mvc 中上传图片

[英]Upload image in asp.net mvc

I use this method to upload image and save it to a folder i created in the server:我使用此方法上传图像并将其保存到我在服务器中创建的文件夹中:

public ActionResult Create(Regional regional, HttpPostedFileBase صورة)
{               

            if (ModelState.IsValid)
            {
  if (صورة != null)
                {
                    صورة.SaveAs(Path.Combine(Server.MapPath("~/1"), صورة.FileName));
                    regional.صورة = صورة.FileName;
                }

                db.Regionals.Add(regional);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(regional);
        }

and the input is:输入是:

<input type="file" id="صورة" name="صورة" accept="image/*" >

But The problem is when i press submit, the name of the image is saved in Database but the picture is not saved to the sever, i can't find it in the folder that i created which is named "1"但问题是当我按下提交时,图像的名称保存在数据库中但图片没有保存到服务器,我在我创建的名为“1”的文件夹中找不到它

please any help请任何帮助

Try this:尝试这个:

        public ActionResult Create(Regional regional, HttpPostedFileBase صورة)
        {               
        if (ModelState.IsValid)
        {
           if (صورة != null)
            {
           var fileName = Path.GetFileName(صورة.FileName); 
                صورة.SaveAs(Path.Combine(Server.MapPath("~/1"),filename));
                regional.صورة = صورة.FileName;
            }

             db.Regionals.Add(regional);
             db.SaveChanges();
             return RedirectToAction("Index");
        }
        return View(regional);
        }

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

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