1

I have created the following view, with having file upload and submit button.

@using (Html.BeginForm("FileUpload", "Home",
                    FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input name="uploadFile" type="file" />
    <input type="submit" value="Upload File" id="btnSubmit" />
}

I have also created the action method in Controller but it gives the null at the "uploadFile"

[HttpPost)]
        public ActionResult FileUpload(HttpPostedFileBase uploadFile)
        {
            if (uploadFile.ContentLength > 0)
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
                                               Path.GetFileName(uploadFile.FileName));
                uploadFile.SaveAs(filePath);
            }
            return View();
        }
Bhupendra Shukla
  • 3,814
  • 6
  • 39
  • 62

4 Answers4

2

Can u try Name with same as uploadFile

In your Page:

@using (Html.BeginForm("FileUpload", "Home",
                    FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input id="uploadFile" name="uploadFile" type="file" />
    <input type="submit" value="Upload File" id="btnSubmit" />
}

As per @Willian Duarte comment : [HttpPost]

In your Code behind:

[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase uploadFile) // OR IEnumerable<HttpPostedFileBase> uploadFile
{
    //For checking purpose 
     HttpPostedFileBase File = Request.Files["uploadFile"];

    if (File != null)
    {
        //If this is True, then its Working.,
    }

    if (uploadFile.ContentLength > 0)
    {
        string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
                                       Path.GetFileName(uploadFile.FileName));
        uploadFile.SaveAs(filePath);
    }
    return View();
}

See here Same question like yours.,

Code project Article about the File upload.,

Community
  • 1
  • 1
RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
  • @BhupendraShukla [Lets continue our discussion here](http://chat.stackoverflow.com/rooms/28436/c-developers) – RajeshKdev Sep 26 '13 at 05:20
  • have you tried instead of `HttpPostedFileBase uploadFile` to `IEnumerable uploadFile` as i mentioned in my answer? – RajeshKdev Sep 26 '13 at 05:23
  • Thanks for the help. Actually there is also no problem in my code, the error is produced due to wrong reference of Helper method i created earlier. Now my code is also running fine. – Bhupendra Shukla Sep 26 '13 at 06:06
  • Setting the id and name worked for me. Only setting the id did not work for me. – James K Nov 17 '13 at 15:59
  • @Wonderboy The `file control name` should be same as `HttpPostedFileBase` parameter name in Controller Action Result. It will work like View Model Concept. Ex : Creating property for the control and getting the value in Controller from View. – RajeshKdev Nov 18 '13 at 11:01
  • @Wonderboy set the name and check it will work., :) I have edited my answer., Hope it helps – RajeshKdev Nov 18 '13 at 11:01
1

create a model and bind it to your view that controller will also expect:

Controller:

    //Model (for instance I've created it inside controller, you can place it in model
    public class uploadFile
    {
        public HttpPostedFileBase file{ get; set; }
    }

    //Action
    public ActionResult Index(uploadFile uploadFile)
    {
        if (uploadFile.file.ContentLength > 0)
        {
            string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
                                           Path.GetFileName(uploadFile.file.FileName));
            uploadFile.file.SaveAs(filePath);
        }
        return View();
    }

View @model sampleMVCApp.Controllers.HomeController.uploadFile

@using (Html.BeginForm("FileUpload", "Home",
                FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  @Html.TextBoxFor(m => m.file, new { type = "file"});  
 <input type="submit" value="Upload File" id="btnSubmit" />
}

Tested Solution!

HTH :)

Sunny Sharma
  • 4,688
  • 5
  • 35
  • 73
1

Try to use (on controller):

var file = System.Web.HttpContext.Current.Request.Files[0];
Willian Andrade
  • 332
  • 3
  • 11
1

Use the following in the controller:

var file = System.Web.HttpContext.Current.Request.Files[0];

Use HttpPost instead of [AcceptVerbs(HttpVerbs.Post)]