why file uploader not works?


i create a sample mvc application to test file upload i read this useful post and do it but client validator works not and gives me error my all codes are:

i attached these in heder tag:

<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery-ui-1.8.24.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>  
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

my model:

public class FIleModel
   [Required, FileExtensions(Extensions = "csv", ErrorMessage = "Specify a CSV file. (Comma-separated values)")]
   public HttpPostedFileBase myFile { get; set; }


Unhandled exception at line 4, column 9003 in http://localhost:6284/Scripts    
   0x800a138f - JavaScript runtime error: Unable to get property 'call' 
   of undefined or null reference

on my view:

 @using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
            <legend>Registration Form</legend>
                <li class="lifile">
                    @Html.TextBoxFor(m => m.myFile, new { type = "file" })
                    @Html.ValidationMessageFor(m => m.myFile)

            <input type="submit" id="btnSubmit" value="Upload" />
asked on Stack Overflow Jul 25, 2013 by motevalizadeh • edited May 23, 2017 by Community

1 Answer


For file uploading, you can use this code.

In controller:

public ActionResult Create(EventModel eventmodel, HttpPostedFileBase file)
   if (ModelState.IsValid)

      //you can validate file here. if okay continue...

      var filename = Path.GetFileName(file.FileName);
      var path = Path.Combine(Server.MapPath("~/Uploads/Photo/"), filename);
      eventmodel.Url = filename;

      return RedirectToAction("Index");
   return View(eventmodel);

And View:

   <input type="file" name="file" id="file" />
   @Html.HiddenFor( model => model.ImageUrl)
   @Html.ValidationMessageFor( model => model.Url )
answered on Stack Overflow Jul 26, 2013 by Jeyhun Rahimov • edited Oct 24, 2016 by Jeyhun Rahimov

User contributions licensed under CC BY-SA 3.0