ajax - Upload files in ASP.NET MVC C# -
i have
[httppost] public actionresult getfiles(httppostedfilebase file) { if (file != null && file.contentlength > 0) { var filename = path.getfilename(file.filename); var path = path.combine(server.mappath("~/app_data/uploads"), filename); file.saveas(path); } return json("uploaded " + request.files.count + " files",jsonrequestbehavior.allowget); }
now want upload files in ajax , save files folder. how files input form , send via ajax?
try this:
cshtml:
<input id="myfile" type="file" name="myfile" onchange="gotopreview(this)" style="display: none"> function gotopreview(input) { if (input.files && input.files[0]) { var tmpimagedata = new filereader(); tmpimagedata.onload = function (e) { setimagedata(imagedata); } tmpimagedata.readasdataurl(input.files[0]); } } function setimagedata(imagedata) { $.ajax({ url: '/home/setimagedata', type: 'post', data: { imagedata: imagedata }, success: function (data) { // code here }, error: function (data) { // code exception } }); }
in setimagedata method @ server side thing.
Comments
Post a Comment