c# - base64 jpeg 到 MemoryStream?
c# - base64 jpeg to MemoryStream?
我有一个像这样的 base64 jpeg:data:image/jpeg;base64 我正在尝试将它转换为 MemoryStream 以便我可以将它上传到 OneDrive....这是我到目前为止得到的:
byte[] frontBytes = Convert.FromBase64String(user.FrontLicense);
using (MemoryStream frontMS = new MemoryStream(frontBytes))
{
await graphClient.Me.Drive.Items[newFolder.Id].ItemWithPath("FrontLicense.jpeg").Content.Request().PutAsync<DriveItem>(frontMS);
}
但是我得到这个错误:
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.
这一行:
byte[] frontBytes = Convert.FromBase64String(user.FrontLicense);
这就是 user.FrontLicense 的样子:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoM.....
我做错了什么?
开头的部分 - data:image/jpeg;base64,
- 不是 base64 数据的一部分。所以你需要先删除它:
const string Base64ImagePrefix = "data:image/jpeg;base64,"
...
if (user.FrontLicense.StartsWith(Base64ImagePrefix))
{
string base64 = user.FromLicense.Substring(Base64ImagePrefix.Length);
byte[] data = Convert.FromBase64String(base64);
// Use the data
}
else
{
// It didn't advertise itself as a base64 data image. What do you want to do?
}
我有一个像这样的 base64 jpeg:data:image/jpeg;base64 我正在尝试将它转换为 MemoryStream 以便我可以将它上传到 OneDrive....这是我到目前为止得到的:
byte[] frontBytes = Convert.FromBase64String(user.FrontLicense);
using (MemoryStream frontMS = new MemoryStream(frontBytes))
{
await graphClient.Me.Drive.Items[newFolder.Id].ItemWithPath("FrontLicense.jpeg").Content.Request().PutAsync<DriveItem>(frontMS);
}
但是我得到这个错误:
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.
这一行:
byte[] frontBytes = Convert.FromBase64String(user.FrontLicense);
这就是 user.FrontLicense 的样子:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoM.....
我做错了什么?
开头的部分 - data:image/jpeg;base64,
- 不是 base64 数据的一部分。所以你需要先删除它:
const string Base64ImagePrefix = "data:image/jpeg;base64,"
...
if (user.FrontLicense.StartsWith(Base64ImagePrefix))
{
string base64 = user.FromLicense.Substring(Base64ImagePrefix.Length);
byte[] data = Convert.FromBase64String(base64);
// Use the data
}
else
{
// It didn't advertise itself as a base64 data image. What do you want to do?
}