[英]How to post and get data uri image from web api?
I am trying to post data uri image from javascript to backend asp.net webapi. 我正在尝试将数据uri图片从javascript发布到后端asp.net webapi。 However, it gives me
input is not a valid Base-64 string
error. 但是,它给我的
input is not a valid Base-64 string
错误。 Now, I understand that it may be due to "data:image/png;base64," part that the data uri contain. 现在,我知道这可能是由于数据uri包含“ data:image / png; base64”这一部分。
Now, even if I remove this part from the data uri and send only the rest of the string to server, how do I store the Base-64 string on the server? 现在,即使我从数据uri中删除了这部分并将仅其余字符串发送到服务器,如何将Base-64字符串存储在服务器上?
Moreover, how to retrieve this data as image from webapi? 此外,如何从webapi检索此数据作为图像?
NOTE: Image would be less than 200kB size and hence is to be stored as varbinary(Max) in sql server. 注意:图像将小于200kB,因此将以varbinary(Max)的形式存储在sql server中。
The thing is you should convert your image to byte[] and store it in your server as varbinary 问题是您应该将图像转换为byte []并将其作为varbinary存储在服务器中
byte []arr = new byte[image1.ContentLength];
image1.InputStream.Read(arr, 0, image1.ContentLength);
While retrieving you should convert the varbinary data to base64 string and base 64 string to image 检索时,应将varbinary数据转换为base64字符串,并将base 64字符串转换为image
string imageBase64Data = Convert.ToBase64String(img);
Here comes the important part the above code convert varbinary to base64string.It should be in proper format to display the image.Thats what the following code does 这是上面的代码将varbinary转换为base64string的重要部分,它应该以正确的格式显示图像,这就是下面的代码的作用
string imageDataURL = string.Format("data:image/png;base64,{0}", asd);
Session["Photo"] = imageDataURL;
Now you can able to view your image 现在您可以查看图像了
Post the image from your client in a for of string, w/o specifying the type. 从客户的图片以字符串for形式发布,不指定类型。 On your action you can get the image in the following way :
您可以通过以下方式获得图像:
var bytes = Convert.FromBase64String(yourStringHere);
using (var ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.