图片上传麻烦

时间:2012-05-13 11:34:57

标签: asp.net-mvc file-upload image-uploading

这是我的上传动作:

[HttpPost]
    public ActionResult addcar(Models.vehicule model)
    {
        ViewBag.cat = new SelectList(entity.categorie, "Idcat", "Nom");
        ViewBag.mark = new SelectList(entity.marque, "id", "nom");      
        if (ModelState.IsValid)
        {
// Upload Function begin
            var destinationFolder = Server.MapPath("/Content/Vpic");
            foreach (string name in Request.Files)
            {
                HttpPostedFileBase postedFile = Request.Files[name];
                if (postedFile.ContentLength > 0)
                {
                    var fileName = model.Matv;
                    var path = Path.Combine(destinationFolder, fileName + ".jpg");
                    postedFile.SaveAs(path);
                }
            }
// upload function end

            model.Idag = User.Identity.Name.ToString();
            entity.vehicule.AddObject(model);
            entity.SaveChanges();
            TempData["Resultat"] = "L'ajout de véhicule a reussi";
            return RedirectToAction("GesV", "Agence");
        }
        else
            return View();

    }

这是我的观点:

 <% using (Html.BeginForm("addcar", "Agence", FormMethod.Post, new { @class = "search_form", @enctype = "multipart/form-data" }))

我已经在其他代码中使用了同样的功能它在这里工作正常我不知道它不起作用,行动的reste工作正常但照片没有上传

1 个答案:

答案 0 :(得分:1)

您的视图是什么样的?您是否在视图中的表单标记上包含属性enctype =“multipart / form-data”?

验证@符号,不需要enctype