使用JavaScript将文件从Base 64转换为Base 64并使用C#将其转换回文件 [英] Convert a file from to Base 64 using JavaScript and converting it back to file using C#
问题描述
我正在尝试使用javascript将pdf和图像文件转换为base 64,并使用WEB API中的C#将其转换回文件。
I am trying to convert pdf and image files to base 64 using javascript and convert it back to file using C# in WEB API.
Javascript
Javascript
var filesSelected = document.getElementById("inputFileToLoad").files;
if (filesSelected.length > 0)
{
var fileToLoad = filesSelected[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textAreaFileContents = document.getElementById("textAreaFileContents");
textAreaFileContents.innerHTML = fileLoadedEvent.target.result;
};
fileReader.readAsDataURL(fileToLoad);
}
C#
Byte[] bytes = Convert.FromBase64String(dd[0].Image_base64Url);
File.WriteAllBytes(actualSavePath,bytes);
但在API中我得到例外 {输入不是一个有效的Base-64字符串,因为它包含一个非基本的64个字符,两个以上的填充字符,或填充字符中的非法字符。}
But in API I'm getting exception as {"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. "}
请告诉我如何继续这个...
谢谢
Please tell me how to proceed with this... Thanks
推荐答案
根据 MDN:FileReader.readAsDataURL 这些生成的网址带有前缀数据:图像/ JPEG; BASE64,
。看看你生成的字符串。查找 base64,
的出现,并获取在此前缀后面开始的base64数据。
According to MDN: FileReader.readAsDataURL those generated URLs are prefixed with something like data:image/jpeg;base64,
. Have a look at your generated string. Look for the occurence of base64,
and take the base64 data that starts after this prefix.
这篇关于使用JavaScript将文件从Base 64转换为Base 64并使用C#将其转换回文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!