试图图像转换为Base64字符串中的JavaScript,并试图转换为C#中的字节数组,给人以base64字符串错误无效字符[复制]

问题描述 投票:0回答:1

这个问题已经在这里有一个答案:

我想一个文件输入图像转换在JavaScript如下:

function getBase64(file) {
    let reader = new FileReader();
    reader.readAsDataURL(file);
    console.log(reader.result);
    return reader.result;
}

使用JSON发送字符串的Web服务,并试图转换为字节数组如下:

byte[] imageBytes = Convert.FromBase64String(base64string);

让用base64字符串错误无效字符。

转换后的字符串数组:pastebin converted string array

Image I am trying to convert

javascript c# base64 converters
1个回答
1
投票

当使用readAsDataURL方法,所述result包含前缀与Data URL模式的data:

MDN web docs

注意:文件的结果导致不能直接解码为Base64字符串。仅检索Base64编码字符串,必须从字符串中删除data:*/*;base64,

所以就像乔纳森·蔡斯评论,你将不得不删除模式的前缀或者在你的JavaScript代码,它发送到你的C#的Web服务或Web服务之前。

© www.soinside.com 2019 - 2024. All rights reserved.