简体   繁体   English

使用Angular发行序列化图像数据

[英]Issue Serializing Image Data with Angular

I have an application where allow a user to select an image, then I use ng-img-crop to allow them to crop the image. 我有一个允许用户选择图像的应用程序,然后使用ng-img-crop允许他们裁剪图像。 From that I have the data for the image represented as something like: 由此,我将图像的数据表示为:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAAAB5fY51AAAgA...

This is set to an image using the ng-src directive, like this: 使用ng-src指令将其设置为图像,如下所示:

<img ng-src="{{product.Image}}" />

From there I am sending the data back to my ASP.NET Web API controller to save the information in the database. 从那里,我将数据发送回我的A​​SP.NET Web API控制器,以将信息保存在数据库中。 My model for the product has a byte[] for the image, which is represented in the database as varbinary. 我的产品模型对图像有一个byte [],它在数据库中表示为varbinary。 Here is my model: 这是我的模型:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string SerialNumber { get; set; }
    public string Notes { get; set; }
    public byte[] Image { get; set; }
    public int CategoryId { get; set; }
    public virtual Category Category { get; set; }
    public ApplicationUser User { get; set; }
}

So, when I send the information back to my controller, here is the request information: 因此,当我将信息发送回控制器时,以下是请求信息:

PUT http://localhost:20697/api/products/2 HTTP/1.1
Host: localhost:20697
Connection: keep-alive
Content-Length: 37272
Accept: application/json, text/plain, */*
Origin: http://localhost:20697
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:20697/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie:  (truncated for size)

{"Category":{"Id":5,"Name":"Graphics/Media","User":null},"Id":2,"Name":"Windows 8 x64 Professional","SerialNumber":"MN8M6","Notes":null,"Image":"data:image/png;base64,iVBORw0KGgo...(truncated for size)","CategoryId":4,"User":null}

The problem, is that if I send real data from the image as shown above, the product is always null when it gets to the controller. 问题是,如果我从上面的图像中发送真实数据,则该产品到达控制器时始终为空。 If I change the data send for the image to a simple string like abcd, then it goes through just fine. 如果我将发送图像的数据更改为像abcd这样的简单字符串,那么它就可以了。 So I am guessing there is a serialization issue. 因此,我猜测存在序列化问题。 Can someone point me to where I am going wrong here? 有人可以指出我在哪里出问题了吗?

Thanks! 谢谢!

The image data is a string. 图像数据是字符串。 You must receive it as a string and convert it back into bytes with Convert.FromBase64String() 您必须将其作为字符串接收,并使用Convert.FromBase64String()将其转换回字节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM