Create json file using blob

前端 未结 2 1077
無奈伤痛
無奈伤痛 2021-02-19 01:56

I have written json code in string and i want to send it using xmlhttp as .json file. Is it possible to do it with blob?

var cleanScript = {
    \'type\': \'scri         


        
2条回答
  •  甜味超标
    2021-02-19 02:13

    Try the code below:

        var int2ByteArray = function(i, minByteCount) {
            var result = [],
                buf = code = +i,
                offsetCount = 0;
            while ((buf = code>>(8 * offsetCount)) || offsetCount < minByteCount) {
                buf = buf & 0xFF;
                ++offsetCount;
                result.push(buf);
            }
            return result.reverse();
        };
    
        var ascii2ByteArray = function(s) {
    
            if (!s) return 0;
            var result = [];
            [].map.call(s, function(c) {
                result = result.concat(int2ByteArray((typeof(c)).toLowerCase() == "number" ? c : c.charCodeAt(0)));
            });
            return result;
        };
    
        // You got the blob here, do whatever you want.
        var blob = new Blob(new Uint8Array(ascii2ByteArray(jsonse)), {type:"text/json"});
    

    The matrix is to convert a string(stringfied by JSON.stringify) in to a Uint8Array that could be used making a blob. I happened make something like that before, hope it's useful.

提交回复
热议问题