ERROR: Convert and Save Base64 String into a File
ERROR: Convert and Save Base64 String into a File
*已编辑
对不起,我可能没说清楚这一点。抛出错误的代码是:
Convert.FromBase64String(base64)
除了上面的方法,还有其他方法在 C# 中将 base64 字符串转换为字节吗?
我需要使用 C# 进行文件转换方面的帮助。我正在尝试将 convert a base64 字符串转换为文件并 save 它进入我的桌面。要保存的文件是 text、excel 或 pdf 文件。我使用的代码是这样的:
File.WriteAllBytes(@"C:\Users\User\Desktop\{thefilename}", Convert.FromBase64String(base64));
但是,我收到一个错误:
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
我尝试先将 base64 保存到文本文件中 然后使用 https://www.freeformatter.com/base64-encoder.html 将其转换以检查 base64 格式是否真的正确。当我这样做时,它将 base64 转换为原始文件.
请问如何解决这个错误?这个错误是由 C# 的限制引起的还是因为我有 base64?我不认为是后者,因为我能够在线将其转换为实际文件。除了我上面使用的方法之外,是否还有其他方法可以将 base64 转换为 C# 中的文件?
我怀疑你的 Base64String
有像 data:image/pdf;base64
这样的前缀。因此,您应该在转换为字节数组之前将其删除。
base64 = base64.Replace("data:image/pdf;base64,","");
File.WriteAllBytes(@"C:\Users\User\Desktop\{thefilename}", Convert.FromBase64String(base64));
我之前尝试将base64
字符串转换为文件到jpg
和pdf
。我认为它也应该适用于其他文件扩展名。试试这个。
var imgByte = Convert.FromBase64String("base64");
var path = "..../file.jpg";
try{
var _FStream = new FileStream(path, FileMode.Create);
_FStream.GetAccessControl();
_FStream.Write(imgByte, 0, imgByte.Length);
_FStream.Close();
_FStream.Dispose();
} catch (Exception ex){
var errorMsg = ex.ToString();
}
如果 base64 字符串的长度不是 4 的倍数,则必须在字符串的右侧填充“=”。即
如果 Base64 字符串的长度为 7 个字符,则我们添加 1 "="s
如果 Base64 字符串是 6 个字符长,那么我们添加 2 "="s
如果 Base64 字符串的长度为 5 个字符,则该字符串已经损坏。所以不可能有base64字符串长度为1、5、11、16等的情况
我可能已经找到了错误的答案。我能够通过打开保存我的 base64 字符串的文本文件来解决错误,然后通过删除填充并再次重新添加来更新它。步骤是这样的:
- 将base64字符串保存到文本文件
- 打开文本文件
- 删除填充
- 保存文件。
- 重新添加删除的填充
- 保存文件。
- 从两次更新的文本文件中获取 base64 字符串
- 调用方式
Convert.FromBase64String(base64String);
我不确定最初是什么导致了这个问题。有谁知道为什么会这样?或者这是 Windows OS 问题、C# 问题还是两者兼而有之?
它可能是一种变体或过时的编码或任何数量的东西
您或许应该阅读 Base64 Wiki
Implementations may have some constraints on the alphabet used for
representing some bit patterns. This notably concerns the last two
characters used in the index table for index 62 and 63, and the
character used for padding (which may be mandatory in some protocols,
or removed in others)
有多种实现和 RFC,请注意 变体摘要 table
由于您不能给我们输入,我们不知道它是什么变体
如果输入的长度正确,那么替换变体字符可能同样容易,再次无法知道,因为看不到输入
string corrected = input.Replace('-', '+').Replace('_', '/');
var decodedBytes = System.Convert.FromBase64String(corrected);
*已编辑
对不起,我可能没说清楚这一点。抛出错误的代码是:
Convert.FromBase64String(base64)
除了上面的方法,还有其他方法在 C# 中将 base64 字符串转换为字节吗?
我需要使用 C# 进行文件转换方面的帮助。我正在尝试将 convert a base64 字符串转换为文件并 save 它进入我的桌面。要保存的文件是 text、excel 或 pdf 文件。我使用的代码是这样的:
File.WriteAllBytes(@"C:\Users\User\Desktop\{thefilename}", Convert.FromBase64String(base64));
但是,我收到一个错误:
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
我尝试先将 base64 保存到文本文件中 然后使用 https://www.freeformatter.com/base64-encoder.html 将其转换以检查 base64 格式是否真的正确。当我这样做时,它将 base64 转换为原始文件.
请问如何解决这个错误?这个错误是由 C# 的限制引起的还是因为我有 base64?我不认为是后者,因为我能够在线将其转换为实际文件。除了我上面使用的方法之外,是否还有其他方法可以将 base64 转换为 C# 中的文件?
我怀疑你的 Base64String
有像 data:image/pdf;base64
这样的前缀。因此,您应该在转换为字节数组之前将其删除。
base64 = base64.Replace("data:image/pdf;base64,","");
File.WriteAllBytes(@"C:\Users\User\Desktop\{thefilename}", Convert.FromBase64String(base64));
我之前尝试将base64
字符串转换为文件到jpg
和pdf
。我认为它也应该适用于其他文件扩展名。试试这个。
var imgByte = Convert.FromBase64String("base64");
var path = "..../file.jpg";
try{
var _FStream = new FileStream(path, FileMode.Create);
_FStream.GetAccessControl();
_FStream.Write(imgByte, 0, imgByte.Length);
_FStream.Close();
_FStream.Dispose();
} catch (Exception ex){
var errorMsg = ex.ToString();
}
如果 base64 字符串的长度不是 4 的倍数,则必须在字符串的右侧填充“=”。即
如果 Base64 字符串的长度为 7 个字符,则我们添加 1 "="s
如果 Base64 字符串是 6 个字符长,那么我们添加 2 "="s
如果 Base64 字符串的长度为 5 个字符,则该字符串已经损坏。所以不可能有base64字符串长度为1、5、11、16等的情况
我可能已经找到了错误的答案。我能够通过打开保存我的 base64 字符串的文本文件来解决错误,然后通过删除填充并再次重新添加来更新它。步骤是这样的:
- 将base64字符串保存到文本文件
- 打开文本文件
- 删除填充
- 保存文件。
- 重新添加删除的填充
- 保存文件。
- 从两次更新的文本文件中获取 base64 字符串
- 调用方式
Convert.FromBase64String(base64String);
我不确定最初是什么导致了这个问题。有谁知道为什么会这样?或者这是 Windows OS 问题、C# 问题还是两者兼而有之?
它可能是一种变体或过时的编码或任何数量的东西
您或许应该阅读 Base64 Wiki
Implementations may have some constraints on the alphabet used for representing some bit patterns. This notably concerns the last two characters used in the index table for index 62 and 63, and the character used for padding (which may be mandatory in some protocols, or removed in others)
有多种实现和 RFC,请注意 变体摘要 table
由于您不能给我们输入,我们不知道它是什么变体
如果输入的长度正确,那么替换变体字符可能同样容易,再次无法知道,因为看不到输入
string corrected = input.Replace('-', '+').Replace('_', '/');
var decodedBytes = System.Convert.FromBase64String(corrected);