ASP.NET MVC > Entity Framework

File upload in ASP.NET MVC

How to upload a file on the server in ASP.NET MVC?

To upload multiple files at a time, read this post.

To upload an image on the server, we need to tweak the Forms element enctype in the Html.BeginForm helper method.

First let’s create a model that will hold the information about the file uploaded.

    public class Files
        public int AutoId { get; set; }

        [StringLength(50, MinimumLength = 4, ErrorMessage = "Must be at least 4
characters long.")]
        public string FileName { get; set; }
        public byte[] FileContent { get; set; }
        public bool Active { get; set; }
        public int PersonalDetailsId {get;set;}

In the above model, we are not going to use FileContent property for now however we have all above corresponding fields into our database table.

The view code looks like below


@model MVCTraining5.Models.Files

    ViewBag.Title = "Create";


@using (Html.BeginForm("Create", "Files", FormMethod.Post, new { enctype = "multipart/form-data" }))
       <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.FileName, htmlAttributes: new { @class =
"control-label col-md-2" })
            <div class="col-md-10">
                <input type="file" name="FileName" id="FileName" class="formcontrol" />
                @Html.ValidationMessageFor(model => model.FileName, "", new { @class
= "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Active, htmlAttributes: new { @class =
"control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.Active)
@Html.ValidationMessageFor(model => model.Active, "", new {
@class = "text-danger" })
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Upload" class="btn btn-default" />

Notice the selected code in between @Html.BeginForm, we have passed custom action method and controller name to submit this form. Apart from this we have also enctype (encryption type) set as an attribute to the form (to multipart/form-data).

After that remaining codes are normal html form elements and normal MVC html helper methods. One small difference is we are using HTML input type=file to provide file upload ability to the user. The name of this element is same as the name in the model property (FileName).


        public ActionResult Create([Bind(Include =
"AutoId,FileName,Active,PersonalDetailsId")] Files files, HttpPostedFileBase FileName)
            if (ModelState.IsValid)

                // upload the file now
                string path = Server.MapPath("~/UserData/");

                // get extension name of the selcted file
                string extensionName =
                // generate a random file name and appende extension name
                string finalFileName = DateTime.Now.Ticks.ToString() +
                // now save the selected file with new name
                FileName.SaveAs(path + finalFileName);

                // set the final file name to the files object so the name would get
saved into the database
                files.FileName = finalFileName;

                return RedirectToAction("Index");

            return View(files);

The controller action method has one extra parameter apart from the Model name and that is HttpPostedFileBase whose name MUST be the name of the input type=file element. This will ensure that the form File element is accessible through this name.

After validating the ModelState, we are getting the path to save, getting the extension name of the user selected file (notice that we have generated a random string as file name and suffixed the extension name to ensure that each uploaded file name is unique on the server) and finally calling the SaveAs method of the HttpPostedFileBase object by passing the path and file name.

Next, we are setting the property of the Files object to the name of the file and calling the SaveChanges() method.

 Views: 22719 | Post Order: 104

Write for us

Hosting Recommendations