我想以ajax形式获取base64字符串值

时间:2018-08-10 10:39:08

标签: javascript json model-view-controller

我想以ajax形式发布此值

<input type="hidden" id="Base" value="iVORw0KGgoAAAANSUh......"/>

输入类型的基数为64字符串

$("form").submit(function (e) {
    var formData = new FormData();
    var BaseImage = $('#Base').val();
    var baseImageContent = BaseImage.replace(/^data:image\/(png|jpg);base64,/, "");
    var blob = baseToBlob(baseImageContent, 'image/png');
    var base = blob;
    var noOfPeople = $("#NoOfPeople").val();
    formData.append('Base64Image', blob);
    if (noOfPeople == '' || officer == ''  || VillageId == '' || DistrictId == '' || BlockId == '') {
        alert("Fill Information...!!!!!!");
    } else {
        $.ajax({
            url: "http://coll.org/API/PukarRegister",
            type: "POST",
            data: formData,
            async: false,
            success: function (msg) {
                alert(msg)   
            },
            cache: false,
            contentType: false,
            processData: false,
        });
    }
    e.preventDefault();
});

1 个答案:

答案 0 :(得分:1)

在Javascript中,您可以使用以下功能对字符串进行编码或解码:

function utf8_to_b64( str ) {
      return window.btoa(unescape(encodeURIComponent( str )));
    }

    function b64_to_utf8( str ) {
      return decodeURIComponent(escape(window.atob( str )));
    }

    // Uso:
    utf8_to_b64('✓ à la mode'); // "4pyTIMOgIGxhIG1vZGU="
    b64_to_utf8('4pyTIMOgIGxhIG1vZGU=');

More information here