Posts

Showing posts with the label base64

toDataURL's output is base64, how to reduce the uploading time and bandwidth of a factor 1/3?

toDataURL's output is base64, how to reduce the uploading time and bandwidth of a factor 1/3? The goal of the following code is to compress the input file (2 MB JPG file => 500 KB file) and then upload it to server when submitting the <form> . <form> When importing an image from a JPG file into a canvas, and exporting it with toDataURL with: toDataURL function doit() { var file = document.getElementById('file').files[0], canvas = document.getElementById('canvas'), hidden = document.getElementById('hidden'), ctx = canvas.getContext("2d"), img = document.createElement("img"), reader = new FileReader(); reader.onload = function(e) { img.src = e.target.result; } img.onload = function () { ctx.drawImage(img, 0, 0); hidden.value = canvas.toDataURL("image/jpeg", 0.5); } reader.readAsDataURL(file); } <input type="file...