简体   繁体   English

如何从Web API发布和获取数据uri图像?

[英]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.

相关问题 如何使用 react-hook-form 将图像数据发布并获取到 Aspnetcore Web api 服务器? - How to post and get the Image Data to the Aspnetcore Web api server using react-hook-form? 如何使用AJAX发送POST数据? 以及如何在Web API中获取POST数据? - How to send POST data with AJAX? And how to get POST data in Web API? 如何将图像二进制文件从 API 调用转换为 Javascript 中的数据 URI? - How can I convert image binary from API call to data URI in Javascript? 如何使用javascript从文件系统映像中获取数据Uri(不是localhost) - How to get Data Uri from a file system image with javascript (localhost is not an option) 如何传递 id 以使用 axios.post 从 api 获取数据? - how to pass id for get data from api with axios.post? 如何将数据从角度发布传递到Web API发布 - How do I pass data from angular post to web api post 图片Javascript中的数据URI - Data URI from Image Javascript 如何使用 Post API 从 LinkedIn 帖子中获取图像 URL? - How can I get the image URL from a LinkedIn post using the Post API? Appscript 中从 post API 获取数据的函数 - A function in Appscript that get data from post API 从 API 获取数据的 POST 请求 - POST-request to get data from API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM