c# - Send image to database using Kendo Ui Upload -
in mvc project , have form gives user possibility upload image kendo ui upload. view:
@using kendo.mvc.ui @model developmentnotesproject.models.noteform @{ viewbag.title = "index"; } <script> $(function () { $("form").kendovalidator(); }); function limitupload() { if ($("span.k-filename").html() != "" || $("span.k-filename").html() != "undefined") { $("div.k-dropzone div.k-button.k-upload-button input").attr('disabled', 'disabled'); } } function enableuploadafterremove() { $("div.k-dropzone div.k-button.k-upload-button input").removeattr('disabled'); } function onsuccess(e) { limitupload(); } function onremove(e) { alert("innn"); enableuploadafterremove(); } $(document).ready(function () { }); </script> <style> form,h2 {margin:0 auto;max-width:900px} </style> <section id="noteform"> <h2>new note save</h2> @using (html.beginform(new { returnurl = viewbag.returnurl })) { @html.antiforgerytoken() @html.validationsummary() <fieldset> <legend>note save</legend> <ol> <li> @html.labelfor(m => m.title) @(html.kendo().textboxfor(m => m.title) .name("title") .value("") ) @html.validationmessagefor(m => m.title) </li> <li> @html.labelfor(m => m.text) @(html.kendo().editorfor(m => m.text) .name("text") ) @html.validationmessagefor(m => m.text) </li> <li> @html.labelfor(m => m.languageid) @(html.kendo().dropdownlistfor(m => m.languageid) .name("languageid") .datatextfield("text") .datavaluefield("value") .bindto((ienumerable<selectlistitem>)viewbag.languageslist) .optionlabel("select language") ) @html.validationmessagefor(m => m.languageid) @*html.validationmessagefor(m => m.language)*@ <!--without kendo--> @*html.dropdownlistfor(p => p.languageid, new selectlist(developmentnotesproject.dal.languageaccess.getlanguages().orderby(c => c.value), "value", "text"), "select country", new { @class = "mydropdownlist" }) @*html.validationmessagefor(m => m.language)*@ </li> <li> @html.labelfor(m => m.img) @(html.kendo().upload() .name("files") .async(a => .save("save", "mynotes") .remove("remove", "mynotes") ) .events(events => events .upload("onsuccess") .remove("onremove")) ) </li> </ol> <input type="submit" value="log in" /> </fieldset> } </section> @section scripts { @scripts.render("~/bundles/jqueryval") }
right now, image saved in add_data folder of project. block of code:
public actionresult save(ienumerable<httppostedfilebase> files) { // name of upload component "files" if (files != null) { foreach (var file in files) { var filename = path.getfilename(file.filename); var physicalpath = path.combine(server.mappath("~/app_data"), filename); file.saveas(physicalpath); } } // return empty string signify success return content(""); }
the problem send form data database when user submits whole form:
[httppost] [allowanonymous] [validateantiforgerytoken] public actionresult add(noteform model, ienumerable<httppostedfilebase> files) { if (modelstate.isvalid) { if (oauthwebsecurity.haslocalaccount(websecurity.getuserid(user.identity.name))) { try { dal.noteaccess.insertnote(model.title, model.text, model.languageid); return redirecttoaction("index", "mynotes"); } catch (membershipcreateuserexception e) { //modelstate.addmodelerror("", errorcodetostring(e.statuscode)); } } else { return redirecttoaction("login", "account"); } } // if got far, failed, redisplay form return view(model); }
i can't find solution image in noteform object (in function above). way, better save image in app_data folder first before sending database or there better approach ? help
edit: model:
[table("note")] public class noteform { [required] [display(name = "title")] public string title { get; set; } [required] [display(name = "text")] public string text { get; set; } [required] [display(name = "language")] public int languageid { get; set; } [foreignkey("languageid")] [uihint("langdropdown")] public virtual language language { get; set; } [display(name = "photo")] public byte[] img { get; set; } [key] [system.web.mvc.hiddeninput(displayvalue = false)] public int id { get; set; } [system.web.mvc.hiddeninput(displayvalue = false)] public int userid { get; set; } }
what problem? have ienumerable<httppostedfilebase> files
parameter of action. files enumerable , set them noteform in proper attributes.
update should switch of async post of files in upload control:
.async(a => a.save("save", "mynotes") .remove("remove", "mynotes")
just remove code before , files parameter of action not null.
Comments
Post a Comment