I'm making an application where my view renders a template with information, then inside my template with Javascript I create a canvas element, I have the data:URI in my Javascript code, but I want to save correctly that image in my MEDIA_ROOT (supposed in /media/report directory) on my server in a Django's way, as I do in another view with a form, but this is not a form! Is a success page where render the template with information and in the same view I have a function that I need that image, maybe my problem can be a little tricky but I will put my template code and my view code to show what I want to do. I don't know if I'm thinking correctly. I really don't need to save that image, just need to use in the function do_something_with_image()
def success(request, report_id): data1 = data2 = '' try: report = Report.objects.get(id=report_id) data1= report.data1 data2= report.data2 #picture_path is /media/report/report_id.png or the file generated in Javascript do_something_with_image(picture_path) except: messages.error(request, "Error") context={ 'report_id': report_id, 'data1': data1, 'data2': data2, } return render(request, 'exito.html', context) Maybe I need to do a POST request?
Here is my template code with the javascript code:
<div id="report"> {{data1}} info - info {{data2}} </div> <script> window.onload = function() { //dom not only ready, but everything is loaded html2canvas(document.getElementById("report")).then(canvas => { let image = canvas.toDataURL("image/png").replace("image/png","image/octet-stream"); let link = document.getElementById('link'); link.setAttribute('download', 'imageIWantToUse.png'); link.setAttribute('href',image) }); }; </script>
html2canvas(document.getElementById("report"))on your server, correct?let image = canvas.toDataURL("image/png").replace("image/png","image/octet-stream");@kyleimage/octet-streamisn't a valid mime type. what's the reasoning behind not using a png?canvas.toDataURL("image/png")only