javascript - pass file as parameter using jquery in asp .net mvc -
in order import data file excel , display data in ascx page, want pass file parameter controller via jquery function here code: view:
<% using (html.beginform (new {enctype = "multipart / form-data"})) {%> <input type = "file" name = "file" /> <input type = "button" value = "ok" onclick = "import (); return false;" /> <%}%>
jquery:
import function () { var formdata = new formdata (); formdata = file var dialog = new messagebox (); $ .ajax ({ type: 'post' url: "/ controller / import" data: formdata, cache: false, contenttype: false, processdata: false, success: function (view) { alert ("success"); $ ("# divlist"). empty () $ ("# mainpagemaincontent") append (view).; }, complete: function () { closepatienter (); dialog.msgclose (); } }); }
controller:
public actionresult import(httppostedfilebase file) { ...... return partialview("otherview", model); }
when execute this, file in controller null can me thank in advance
you not adding file formdata obj. this:
var formdata = new formdata (); //formdata = file var uploadfield = document.getelementbyid('uploadfield'); formdata.append("file", uploadfield.files[0]);
where "uploadfield" id of file input element.
Comments
Post a Comment