1.canvas转换为dataURL (从canvas获取dataURL)

var dataurl = canvas.toDataURL('image/png');

var dataurl2 = canvas.toDataURL('image/jpeg', 0.8);

2.File对象转换为dataURL、Blob对象转换为dataURL

function readBlobAsDataURL(blob, callback) {
   var a = new FileReader();
   a.onload = function(e) {callback(e.target.result);};
   a.readAsDataURL(blob);
}
//example:
readBlobAsDataURL(blob, function (dataurl){
   console.log(dataurl);
});
readBlobAsDataURL(file, function (dataurl){
   console.log(dataurl);
});

3.dataURL转换为Blob对象、dataURL转换为File对象

function dataURLtoBlob(dataurl) {
   var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
       bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
   while(n--){
       u8arr[n] = bstr.charCodeAt(n);
   }
   return new Blob([u8arr], {type:mime});
}
function dataURLtoFile(dataurl, filename) {
   var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
       bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
   while(n--){
       u8arr[n] = bstr.charCodeAt(n);
   }
   return new File([u8arr], filename, {type:mime});
}
//test:
var blob = dataURLtoBlob('data:text/plain;base64,YWFhYWFhYQ==');
var file = dataURLtoFile('data:text/plain;base64,YWFhYWFhYQ==', 'test.txt');

4.dataURL图片数据绘制到canvas

var img = new Image();
img.onload = function(){
    canvas.drawImage(img);
};
img.src = dataurl;

5.File,Blob的图片文件数据绘制到canvas

readBlobAsDataURL(file, function (dataurl){
   var img = new Image();
   img.onload = function(){
       canvas.drawImage(img);
   };
   img.src = dataurl;
});

6.Canvas转换为Blob对象并使用Ajax发送

var dataurl = canvas.toDataURL('image/png');
var blob = dataURLtoBlob(dataurl);
//使用ajax发送
var fd = new FormData();
fd.append("image", blob, "image.png");
var xhr = new XMLHttpRequest();
xhr.open('POST', '/server', true);
xhr.send(fd);