Request.files is Empty in MVC file upload

前端 未结 3 846
你的背包
你的背包 2020-12-18 12:58

I have the same issue

@using (Html.BeginForm(\"CreateRequest\", \"SupportRequest\", FormMethod.Post, new { id = \"f         


        
3条回答
  •  盖世英雄少女心
    2020-12-18 13:40

    I did the different way using form collection that can easily Upload files.Like u did add that line inside begin form in cshtml page.

    public ActionResult Create([Bind(Include="Id,Name,Pic,ActiveStatus")] FormCollection form)
        {
    
            string Pic = "";
            var file = Request.Files[0];
    
            if (file.ContentLength > 0)
            {
                Pic = string.Format("~/Uploads/Category/{0}", file.FileName);
                file.SaveAs(HttpContext.Server.MapPath(Pic));
    
            }
    
            ItemCategory obj = new ItemCategory
            {
                Name = form["Name"].ToString(),
                Pic = file.FileName
    
            };
    
            db.ItemCategories.Add(obj);
            db.SaveChanges();
            return RedirectToAction("Index");
    

提交回复
热议问题