File Upload ASP.NET MVC (getting error) -
i trying add together feature asp.net web application allow user upload file contains list of courses student. added upload feature view , created new method in controller. every time select file , click upload, next error: "an exception of type 'system.nullreferenceexception' occurred". guessing file variable coming null? below controller code , view code:
view:
<div id="uploadcourselist"> @using (html.beginform("uploadcourselist", "managestudentcourses", formmethod.post, new { enctype = "multipart/form-data" })) { @html.antiforgerytoken() @html.textboxfor(x => model.userid, @model.userid, new { @class = "addcoursetxtboxid" }) <table class="courselisttable"> <tr> <th colspan="4"> upload course of study list </th> </tr> <tr> <td> select file: </td> <td> <input type="file" name="file" id="file" /> </td> <td> <input type="submit"> </td> </tr> </table> } </div>
controller
public partialviewresult uploadcourselist(httppostedfilebase file, courselistviewmodel modelview) { if (file.contentlength > 0 && file != null) { string path = path.combine(server.mappath("~/content/courselist"), path.getfilename(file.filename)); file.saveas(path); } homecoming partialview("~/views/managestudentcourses/listcourses.cshtml"); }
you have 2 main problems here:
the attribute on form tag shouldenctype
, not encrypt
. you cannot send files using ajax.beginform, @ to the lowest degree not @ think. quick solution utilize html.beginform() instead, if unacceptable, need @ third-party plugin illustration plupload. asp.net-mvc file-upload asp.net-ajax
No comments:
Post a Comment