jquery - uploading image via ajax from cropit plugin -


i using jquery cropit plugin have user upload , crop image. once image cropped liking click "upload" button , supposed save server in specified folder. able save cropped image in javascript variable using cropit's export function. have output window , verified working. when make ajax call send controller save server, can't figure out how image. request.files empty , imagefile null. using mvc , asp.net.

html:

<div id="image-cropper">     <div class="cropit-image-preview-container">         <div class="cropit-image-preview"></div>     </div>     <div class="slider-wrapper">         <span class="icon icon-image small-image"></span>         <input class="cropit-image-zoom-input custom" type="range" min="0" max="1" step="0.01">         <span class="icon icon-image large-image"></span>         </div>         <div class="btns">             <!-- actual file input hidden -->             <!-- , clicking on button open select file dialog -->             <input class="cropit-image-input custom" type="file" accept="image/*">             <input type="hidden" class="hidden-image-data" />             <div class="btn select-image-btn">                 <span class="icon icon-image"></span>                 select new image             </div>             <div class="btn upload-btn">                 <span class="icon icon-box-save"></span>                 upload cropped image             </div>         </div>     </div> 

javascript:

$('#image-cropper').cropit({             imagebackground: true,             onimageloading: function () {                 // these lines needed center background image match main cropped image                 $(".cropit-image-preview-container").css("width", "500px");                 $(".cropit-image-background-container").css("left", "51px");             }         });          // when user clicks select image button,         // open select file dialog programmatically         $('.select-image-btn').click(function () {             $('.cropit-image-input').click();         });         $('.upload-btn').click(function () {             var imagedata = $('#image-cropper').cropit('export', {                 type: 'image/jpeg'             });             $('.hidden-image-data').val(imagedata);             var formdata = new formdata();             formdata.append("imagefile", imagedata);             var token = $('input[name="__requestverificationtoken"]').val();             var headers = {};             headers['__requestverificationtoken'] = token;             $.ajax({                 url: '@url.action("uploadimage", "admin")',                 //headers: headers,                 data: formdata,                 type: "post",                 contenttype: false,                 processdata: false,                 success: function (response) {                     console.log(response);                 },             });         }); 

controller action:

[httppost] //[validateantiforgerytoken] public bool uploadimage(httppostedfilebase imagefile) {     bool saved = false;     foreach(string file in request.files)     {         var filecontent = request.files[file];     }     if (imagefile != null)     {         // validate uploaded image(optional)         // complete file path         var filesavepath = path.combine(system.web.httpcontext.current.server.mappath("~/content/img/products"), imagefile.filename);          // save uploaded file "uploadedfiles" folder         imagefile.saveas(filesavepath);         if (system.io.file.exists(filesavepath))         {             saved = true;         }     }     return saved; } 

you trying save base 64 data directly file. cropit "export" function returns data in datauri format has real binary data encoded in base 64.

you code alright point trying save data file. use base 64 decoder decode first before saving.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -