c# - Couldn't Upload File ASP.NET MVC -
i'm trying create create model object page. i'm using asp.net mvc c#. in project i'm trying create library application has books , can add book application has picture. create controller this,
[httppost] public actionresult create([bind(exclude = "id,bringback,borrower,isborrowed")]book booktocreate, httppostedfilebase picture1) { try { if (picture1.contentlength > 0) { var filename = path.getfilename(picture1.filename); var path = path.combine(server.mappath("~/app_data/pictures"), filename); picture1.saveas(path); } // todo: add insert logic here _entities.addtolibrary(booktocreate); _entities.savechanges(); return redirecttoaction("listbooks"); } catch { return view(); } } and view this,
<%@ page language="c#" inherits="system.web.mvc.viewpage<mvcapplication2.models.book>" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>create</title> </head> <body> <% using (html.beginform(new { enctype = "multipart/form-data" })) {%> <%: html.validationsummary(true) %> <fieldset> <legend>fields</legend> <div class="editor-label"> <%: html.labelfor(model => model.name) %> </div> <div class="editor-field"> <%: html.textboxfor(model => model.name) %> <%: html.validationmessagefor(model => model.name) %> </div> <div class="editor-label"> <%: html.labelfor(model => model.author) %> </div> <div class="editor-field"> <%: html.textboxfor(model => model.author) %> <%: html.validationmessagefor(model => model.author) %> </div> <div class="editor-label"> <%: html.labelfor(model => model.picture) %> </div> <div class="editor-field"> <%: html.textboxfor(model => model.picture) %> <%: html.validationmessagefor(model => model.picture) %> </div> <input type='file' name='picture1' id='picture1' /> <p> <input type="submit" value="create" /> </p> </fieldset> <% } %> <div> <%: html.actionlink("back list", "index") %> </div>
i have tried many things couldn't upload file. can do?
try getting posted file(s) request instead.
var picture1 = this.request.files["picture1"];
Comments
Post a Comment