Compressing base64 data uri images

后端 未结 3 568
小蘑菇
小蘑菇 2020-12-13 13:39

Problem

I\'m creating multiple charts that are then sent to the server to create a PDF. This data can get large.

Question

3条回答
  •  既然无缘
    2020-12-13 14:14

    I finally got this working with fairly large files.

    To do it, I used lz-string, as shown above.

    There is one issue though, which is that sending UTF-16 data via ajax is deprecated, so.. it doesn't work. The fix is to convert to and from UTF-16 and UTF-8.

    Here's the conversion code I'm using. I'm not sure what the standard endian is, btw:

    var UTF = {};
    
    UTF.U16to8 = function(convertMe) {
    var out = "";
      for(var i = 0; i < convertMe.length; i++) {
          var charCode = convertMe.charCodeAt(i);
          out += String.fromCharCode(~~(charCode/256));
          out += String.fromCharCode(charCode%256);
        }
      return out;
    }
    
    UTF.U8to16 = function(convertMe) {
      var out = ""
      for(var i = 0; i < convertMe.length; i += 2) {
        var charCode = convertMe.charCodeAt(i)*256;
        charCode += convertMe.charCodeAt(i+1);
        out += String.fromCharCode(charCode)
      }
      return out;
    }
    

    If you're using Node.js, this code works on both sides. If not, you'll have to write your own version of these converters for the server side.

    Phew! What a day this was.

提交回复
热议问题