将base64字符串转换为字节数组

时间:2019-01-23 08:33:14

标签: javascript jquery ajax asp.net-mvc base64

在页面上,您需要加载输入类型文件,并在img中显示此图像。然后ajax请求将base64字符串传输到服务器,然后将其转换为字节数组。这是我的查看代码:

$('#modalWindow').on('click', '#send', function () {
    var file = $('#autoPicture').attr('src');

    var formData = new FormData();
    formData.append("base64img", file);

    $.ajax({
        type: "POST",
        url: "/Home/LoadImage",
        data: $('form').serialize() + "&base64img=" + formData.get('base64img'),
        success: function (data) {
            alert('Success');
        },
        error: function (error) {
            alert(error.responseText);
        }
    });
});

控制器代码:

public ActionResult LoadImage(Car car, string base64img)
    {
        var base64arr = base64img.Split(',');
        byte[] picture = Convert.FromBase64String(base64arr[1]);
        return View();
    }

但是当您尝试使用以下文字转换崩溃时:

  

该输入不是有效的Base-64字符串,因为它包含Base 64以外的编码格式的字符,两个以上的填充字符或填充字符中的无效字符

尽管如果您使用该站点将base64转换为图像,则可以正常转换。 https://codebeautify.org/base64-to-image-converter

如果您需要编写示例base64代码,我会给出。很大我可以给出例如代码的开头和结尾,根据我的定义,是否合适。好吧,总的来说,如果您需要一个例子,我会给出。我不明白为什么c#无法转换。

该方法不适合的第二张图片:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAtAC0AAD//gBRRmlsZSBzb3VyY2U6IGh0dHA6Ly9jb21tb25zLndpa2ltZWRpYS5vcmcvd2lraS9GaWxlOlZ3LXNjaXJvY2NvLW1vZGVsbC0yMDEwLmpwZ//bAEMABgQFBgUEBgYFBgcHBggKEAoKCQkKFA4PDBAXFBgYFxQWFhodJR8aGyMcFhYgLCAjJicpKikZHy0wLSgwJSgpKP/bAEMBBwcHCggKEwoKEygaFhooKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKP/AABEIAKUA3AMBIgACEQEDEQH/xAAdAAABBAMBAQAAAAAAAAAAAAACAAEDBAUGCAcJ/8QASBAAAgEDAgMFBQQFCAgHAA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cjG2xFCyRhs z0zkGrQjz0PXYbUwi/z0p0Kyv3fLuqYyMj4VH9lUnLFh4kHx9avNEh5SwHMpyCNsUxxsM5zvscUahsU 5VQCNuhx1yBQmNChHIQpGdvM1bKDP3SAaSxqB7W/jv8AnTSFZUCoGUAvtsPDPnmkQDgq3jsDVnkwMKcbZJzQEH3j1z4LVCsgdDyjcnxIU4 uah7rP 8/suR W1XHBCllGWHTOBUDvynHsj45pisyQh5ZpYyVIQKfdAyG8KiidJLoRPEhKg 1656jypUq5LZrXRce3jAU 0WwWO/kSKGaNYy7ADAXOMUqVNSdjcVVkwhUWzSsAQhG2MZBz4/KojEhRmCrjlyBjpk0qVGzFSEsK8xzjHMVHp/nFBOgQIMKSzlc4pUqpSYqQUkHsLytgnxKg7ZXp9aL7PvgORkdcdMGlSqrECYsMPaO fCm7hSV9c9fClSpoQlHMuBt1P0pih5iQ2D4bdKVKkxiAwAdsjxx60A3OPxpUqBgkkEYx9KZkICgtnP76VKnYhBjjqdt/wAadckjJPWlSp xDY6eR3piABkDptSpUAPjrv5UOfbYeWAM/ClSqvQhAZ32pjtSpUIQzpuRnw8RVdkBPRcdB7NKlQyl2f/Z

第一张图片的base64行很大,它也关闭了吗?

1 个答案:

答案 0 :(得分:1)

问题似乎源于此行:

byte[] picture = Convert.FromBase64String(base64img);

如果base64img包含某些标题,例如data:image/png;base64,则Convert.FromBase64String()会失败,因为该字符串未完全以Base64格式格式化。从字符串参数转换为删除相应的Base64标头之前,请考虑使用string.Split()

[HttpPost]
public ActionResult LoadImage(Car car, string base64img)
{
    // split the header and content, separated by comma
    var base64arr = base64img.Split(',');
    byte[] picture = Convert.FromBase64String(base64arr[1]);

    // do something with the image

    return View();
}

如果内容字符串包含ASCII或尾随填充字符以外的其他字符,则应按以下方式删除它们:

string base64str = base64arr[1];
string processed = base64str.Replace('_', '/').Replace('-', '+');

switch (base64str.Length % 4) 
{
    case 2: 
       processed += "=="; 
       break;
    case 3: 
       processed += "="; 
       break;
}

byte[] picture = Convert.FromBase64String(processed);

在传递以Base64字符串格式化的图像时,还应考虑使用FormData对象:

$('#modalWindow').on('click', '#send', function () {
    var file = $('#autoPicture').attr('src');

    // note: if you send other form data, use 'serializeArray()' or 'serialize()'

    var formData = new FormData();
    // append other form contents here
    formData.append("base64img", file);

    $.ajax({
        type: "POST",
        url: "/Home/LoadImage",
        data: formData,
        success: function (data) {
            alert('Привет');
        },
        error: function (error) {
            alert(error.responseText);
        }
    });
});

注意:

Base64编码的字符串末尾的===等号用作尾随填充字符,并非所有以Base64编码的字符串都以它们结尾。您可以看到填充here背后的原因。