簡體   English   中英

在ASP.NET MVC中上傳文件

[英]upload files in asp.net mvc

我想在asp.net mvc中上傳文件。 我有以下代碼。 但是file始終返回null值。 有什么我應該嘗試嗎?

.cshtml

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

controller.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");
    }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM