![](/img/trans.png)
[英]How to post and get the Image Data to the Aspnetcore Web api server using react-hook-form?
[英]How to post and get data uri image from web api?
我正在尝试将数据uri图片从javascript发布到后端asp.net webapi。 但是,它给我的input is not a valid Base-64 string
错误。 现在,我知道这可能是由于数据uri包含“ data:image / png; base64”这一部分。
现在,即使我从数据uri中删除了这部分并将仅其余字符串发送到服务器,如何将Base-64字符串存储在服务器上?
此外,如何从webapi检索此数据作为图像?
注意:图像将小于200kB,因此将以varbinary(Max)的形式存储在sql server中。
问题是您应该将图像转换为byte []并将其作为varbinary存储在服务器中
byte []arr = new byte[image1.ContentLength];
image1.InputStream.Read(arr, 0, image1.ContentLength);
检索时,应将varbinary数据转换为base64字符串,并将base 64字符串转换为image
string imageBase64Data = Convert.ToBase64String(img);
这是上面的代码将varbinary转换为base64string的重要部分,它应该以正确的格式显示图像,这就是下面的代码的作用
string imageDataURL = string.Format("data:image/png;base64,{0}", asd);
Session["Photo"] = imageDataURL;
现在您可以查看图像了
从客户的图片以字符串for形式发布,不指定类型。 您可以通过以下方式获得图像:
var bytes = Convert.FromBase64String(yourStringHere);
using (var ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.