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

孤街醉人 提交于 2020-01-16 18:04:39

问题


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>.

When importing an image from a JPG file into a canvas, and exporting it with toDataURL with:

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" onchange="doit();" id="file" />

<form action="/upload" method="post">
<input type="hidden" id="hidden" />
<input type="submit" />
</form>

<canvas id="canvas" style="display: none" />

it works, but the output hidden field in the <form> is base64-encoded, i.e. something like:

data:image/jpeg;base64,/9j/4AAQSkZJRgAB...

It is well known that base64 uses 1.3333 times the normal size of binary data.

Question: how to avoid to waste 1/3 of data in uploading time (client => server) and bandwidth in this case, i.e. when submitting the <form>?

Note: I think the problem will be the same if I use AJAX instead of <form> submission, isn't it?


回答1:


var jsForm = null;

function doit() {
    var file = document.getElementById('file').files[0],
        canvas = document.getElementById('canvas'),
        ctx = canvas.getContext("2d"),
        img = document.createElement("img");

    img.src = window.URL.createObjectURL(file);
    
    img.onload = function () {
        if (!jsForm) {
          jsForm = new FormData();
        }
        ctx.drawImage(img, 0, 0);
        canvas.toBlob(function(blob) {
          jsForm.set('image', blob, file.name);
        }, "image/jpeg", 0.5);
    }
}

var form = document.getElementById('form');
form.onsubmit = function(e) {
  e.preventDefault();
  if (!jsForm) return;
  var request = new XMLHttpRequest();
  request.open(this.method||'POST', this.action||'/');
  request.send(jsForm);
  jsForm = null;
}
<form method="POST" action ="/upload" id="form">
  <input type="file" onchange="doit();" id="file" />
  <button>Submit</button>
</form>
<canvas id="canvas" style="display: none" />


来源:https://stackoverflow.com/questions/51118927/todataurls-output-is-base64-how-to-reduce-the-uploading-time-and-bandwidth-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!