0

我想在 asp.net mvc 中上传一个文件。我有以下代码。但file回报总是null价值。有什么,我应该试试?

.cshtml

<input type="file" name="file" id="file" />

控制器.cs

   [HttpPost]
    public ActionResult Index(BookModel model, HttpPostedFileBase file, FormCollection values)
    {
        try
        {
            if (!ModelState.IsValid)
            {
                return View("Index", new BookModel());
            }

            if (file != null && file.ContentLength > 0)
            {                    
                var fileName = Path.GetFileName(file.FileName);                    
                var path = Path.Combine(Server.MapPath("~/img/"), fileName);
                file.SaveAs(path);

                model.ImageUrl = fileName;
            }


         //SendMail();
        }
        catch (Exception ex)
        {               
            return View("Index", new BookModel());
        }

        return View("Success");
    }
4

1 回答 1

3

添加到表单enctype="multipart/form-data"

于 2013-05-13T08:18:53.303 回答