简体   繁体   中英

Django - Save base64 image from template to static

I was wondering if it's possible to save an generated image from my template to the MEDIA_URL? My image is generated in base64, I would like to give it a name and save it as png or jpeg to the MEDIA_URL

<img id="canvasImg" style="display:none;" src="data:image/png;base64,iVB...">

My image is generated using the html2canvas script. I then use this function to transform the canvas into an image:

function canvas2img(){
      html2canvas(document.getElementById("bg"), {
        onrendered: function(canvas) {      
          canvas.setAttribute("id", "canvas");
          var dataURL = canvas.toDataURL('image/png', 1.0);
          document.getElementById('canvasImg').src = dataURL;  
        }
      })
   };

Thank you!

I won't post the full code here just some guidance, everything from StackOverflow:

First you need to send your base64 image to django using AJAX: https://stackoverflow.com/a/13198699/263989

Then get the base64 in an AJAX function:

from django.http import HttpResponse
def get_bas64(request):
    if request.is_ajax():
        # process the image
        return HttpResponse('')

To convert the base64 string to an image using PIL https://stackoverflow.com/a/19911883/263989

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM