Binding HttpPostedFileBase using Ajax.BeginForm

前端 未结 4 559
星月不相逢
星月不相逢 2020-12-03 19:16

I have a form which binds a model and a file upload using the default binder for HttpPostedFileBase.

This works fine when using Html.BeginForm(). However, I wanted t

4条回答
  •  鱼传尺愫
    2020-12-03 20:01

    ADD id="file" in your tag input

    IN YOUR ACTIONRESULT PARAMETER HttpPostedFileBase 'file' name and view tag name should be same

            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create(tbl_products tbl_products,HttpPostedFileBase file)
            {
                if (ModelState.IsValid)
                {
                    tbl_products.phototype = file.ContentType;
                    tbl_products.photo =new byte[file.ContentLength ];
                    file.InputStream.Read(tbl_products.photo,0, file.ContentLength);
    
                    if(obj.insert(tbl_products))
                    {
                    return RedirectToAction("Index");
                    }
                    else
                    {
                        return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
                    }   
                }
    
                return View(tbl_products);
            }
    

    IT WORKS FOR ME

提交回复
热议问题