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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -