Request.files is Empty in MVC file upload

前端 未结 3 835
你的背包
你的背包 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:24

    Since you are creating the post of the data you will need to post the file data. see this post:

    How can I upload files asynchronously?

    0 讨论(0)
  • 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");
    
    0 讨论(0)
  • 2020-12-18 13:45

    You need to use HttpPostedFileBase in controller action parameters inorder to get the filedata that you have posted.

    Please read this full article by Phil Haack

    0 讨论(0)
提交回复
热议问题