asp.net mvc 3 - Upload File via MVC3 -
i'm trying upload file via mvc3 view :
@{ viewbag.title = "pic"; layout = "~/areas/admin/views/shared/_adminlayout.cshtml"; } @using (html.beginform("fileupload", "blog", formmethod.post)) { <input name="uploadfile" type="file" /> <input type="submit" value="upload file" /> }
and actions :
public actionresult fileupload() { return view(); } [acceptverbs(httpverbs.post)] 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(); }
what wrong code ?? have error
object reference not set instance of object.
in line
if (uploadfile.contentlength > 0)
you should add enctype attribute form.
@using (html.beginform("fileupload", "blog", formmethod.post, new { enctype = "multipart/form-data" }))
Comments
Post a Comment