c# - Order of calling methods -
i'm trying implment file upload feature users can upload multiple files @ once. i'm using dropzone.js on client side , c# in end. i'm using mvc razor views , have view user inputs details , upload files within same page. i've gone ahead , created following methods
[httppost] public async task<actionresult> uploadedfile(int? id) { foreach (string filename in request.files) { httppostedfilebase file = request.files[filename]; if (file != null && file.contentlength > 0) { //upload file here //file gets uploaded } } return new emptyresult(); }
i need store model details db done in method follows
public async task<actionresult> savetodb(myviewmodel viewmodel) { if (modelstate.isvalid) { //store db here int? id = viewmodel.user.userid; //redirect home page if ok } return view(viewmodel); }
now question need run savetodb
before uploadedfile
method because want pass int? id
param because use @ end of file name. can't seem figure out how go this. client jquery
looks this:
<script> dropzone.autodiscover = false; var mydropzone = new dropzone("#dzupload",{ url: "/home/uploadedfile", autoprocessqueue: false }); $("#submit-all").on('click', function () { mydropzone.options.autoprocessqueue = true; mydropzone.processqueue(); }); mydropzone.on("addedfile", function (file) { var removebutton = dropzone.createelement("<button class='btn btn-danger'>remove file</button>"); var dlink = mydropzone; // listen click event removebutton.addeventlistener("click", function (e) { // make sure button click doesn't submit form: e.preventdefault(); e.stoppropagation(); // remove file preview. dlink.removefile(file); }); // add button file preview element. file.previewelement.appendchild(removebutton); }); </script>
note: #submit-all
button submits entire form
you dont need define different method uploading, can e done in same post action :
<form action="~/home/savedropzonejsuploadedfiles" class="dropzone" id="dropzonejsform"></form> <button id="submit-all">submit files</button> </div> @section scripts { <script type="text/javascript"> dropzone.options.dropzonejsform = { //prevents dropzone uploading dropped files autoprocessqueue: false, init: function () { var submitbutton = document.queryselector("#submit-all"); var mydropzone = this; //closure submitbutton.addeventlistener("click", function () { //tell dropzone process queued files mydropzone.processqueue(); }); } }; </script>
}
now on "savedropzonejsuploadedfiles" action method first handle db saving code upload files.. action method can take model parameter
Comments
Post a Comment