asp.net mvc - Why can't save image to database in this way? -
i want save image , other information databse in asp.net mc3 project. i've saved image database before , worked. code in controller this:
public actionresult savetodb() { if (request.files.count > 0 && request.files[0] != null) { httppostedfilebase file = request.files[0]; var path = path.combine(server.mappath("~/content/image"), file.filename); file.saveas(path); byte[] buffer = system.io.file.readallbytes(path); myad.adimage = buffer; storedb.addtoads(myad); storedb.savechanges(); } return view(); } }
now changed table , want save other information more image database. code this:
public actionresult savetodb(advertiseview model) { if (request.files.count > 0 && request.files[0] != null) { httppostedfilebase file = request.files[0]; var path = path.combine(server.mappath("~/content/image"), file.filename); file.saveas(path); byte[] buffer = system.io.file.readallbytes(path); myad.adimage = buffer; } myad.adtitle = model.adtitle; myad.adcontext = model.context; myad.adscope = model.scope; storedb.addtoads(myad); storedb.savechanges(); return view(); }
there isn't problem other infos image cant saved. understand
request.files.count
return 0. don't know should now. can me please? alot.
i'd use view model.
let's suppose have domain model first:
public class mydomainmodel { public byte[] adimage { get; set; } public string description { get; set; } }
then define view model:
public class myviewmodel { [required] public httppostedfilebase file { get; set; } [datatype(datatype.multilinetext)] public string description { get; set; } }
a controller:
public class homecontroller : controller { public actionresult index() { return view(new myviewmodel()); } [httppost] public actionresult index(myviewmodel model) { if (!modelstate.isvalid) { return view(model); } // todo: move mapping logic // mapping layer avoid polluting controller // recommend automapper purpose // http://automapper.org/ using (var stream = new memorystream()) { model.file.inputstream.copyto(stream); var image = stream.toarray(); var domainmodel = new mydomainmodel { adimage = image, description = model.description }; // todo: persist domain model passing method // on dal layer } return content("thanks submitting"); } }
and once recommended refactoring complete:
public class homecontroller : controller { public actionresult index() { return view(new myviewmodel()); } [httppost] public actionresult index(myviewmodel model) { if (!modelstate.isvalid) { return view(model); } mydomainmodel domainmodel = mapper.map<myviewmodel, mydomainmodel>(model); // todo: persist domain model passing method // on dal layer return content("thanks submitting"); } }
and view allow user upload file:
@using (html.beginform(null, null, formmethod.post, new { enctype = "multipart/form-data" })) { <div> @html.labelfor(x => x.description) @html.editorfor(x => x.description) </div> <div> @html.labelfor(x => x.file) @html.textboxfor(x => x.file, new { type = "file" }) @html.validationmessagefor(x => x.file) </div> <button type="submit">ok</button> }
Comments
Post a Comment