asp.net - Upload file using jQuery and post it to Controller -
i'm wondering if possible @ upload file posting controller action in asp.net mvc. dialog upload form dynamically generated , inside jquery dialog in case.
i'm aware of file input element i'm not sure how send file controller action, not sure how set action
parameter
your action should this:
[httppost] public actionresult upload(httppostedfilebase file) { if (file.contentlength > 0) { var filename = path.getfilename(file.filename); var path = path.combine(server.mappath("~/app_data/uploads"), filename); file.saveas(path); } return redirecttoaction("index"); }
taken :http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/
then using jquery dialog file upload:
$dialog.dialog("option", "buttons", { "save": function () { var dlg = $(this); var formdata = new formdata($("#" + formname)[0]); $.ajax({ url: /controller/upload, type: 'post', data: formdata, processdata: false, contenttype: false, success: function (response, textstatus, xhr) { ... } }, error: function (xhr, status, error) { .... } }); }, "cancel": function () { $(this).dialog("close"); $(this).empty(); } });
Comments
Post a Comment