繁体   English   中英

ASP.Net jQuery网络摄像头图像到数据库varbinary(max)

[英]ASP.Net jQuery webcam image to database varbinary(max)

我已经编写了Javascript来捕获网络摄像头中的图像,然后将其序列化到我的SQL Server数据库中。 数据库中的字段是通常的varbinary(max)。 我从萤火虫中可以看到原始图像正在传递给我的Web方法,但它始终抛出以下错误:

“'797719006-'的插入失败:Telerik.OpenAccess.RT.sql.SQLException:无法将值NULL插入表'GoldStar.Students.Pictures'的'Picture'列中;

捕获图像的javascript绝对可以正常工作,所以我知道那里没有问题,只是将其保存到数据库中即可。 也许我通过ajax通过网络方法进行更新使事情变得过于复杂了???

我在注册页面上创建了一个启用Web的方法,它看起来像这样:

    [WebMethod(EnableSession = true)]
    public static void UpdatePicture(object picture)
    {
        //Check if the user already has a picture in the database.
        using (var dataContext = new DAL.GoldStarModel())
        {
            var userPictures = (from p in dataContext.Pictures
                                where p.UserId == (Guid) Membership.GetUser().ProviderUserKey
                                select p);

            if (userPictures.Count() > 0)
            {
                //If user already has a picture then update.
                userPictures.First().Picture = picture as byte[];
            }
            else
            {
                //If User does NOT have a picture then add one.
                dataContext.Add(new Students.Pictures()
                                    {
                                        UserId = (Guid) Membership.GetUser().ProviderUserKey,
                                        Picture = picture as byte[],
                                        CreationDate = DateTime.Now

                                    });
            }

            dataContext.SaveChanges();
        }

相关的Javascript如下:

                var canvas = document.getElementById("canvas");
                var profilePic = $('#profilePic')

                $('#webcam').hide();

                try {
                    //Update the profile picture in the parent.
                    var currentPic = self.parent.document.getElementById('MainContent_UserFormView_RegisterUser_StudentPicture');

                    var oldWidth = Number(currentPic.width);
                    var oldHeight = Number(currentPic.height);

                    currentPic.src = canvas.toDataURL();

                    currentPic.width = oldWidth;
                    currentPic.height = oldHeight;

                    profilePic.show();

                    $.ajax({
                        type: "POST",
                        url: "/Account/Register.aspx/UpdatePicture",
                        contentType: "application/json; charset=utf-8",
                        data: "{'picture': '" + currentPic.src + "'}",
                        dataType: "json",
                        success: function (result) {
                            //ApplyTemplate(result)
                        }
                    });
                }

您是否使用过提琴手之类的工具来检查线路上发生了什么,因为乍一看,它只是图像的字符串路径,而不是图像数据。

在服务器上,您不能仅将对象强制转换为字节数组,除非您已经拥有一个字节数组,在这种情况下,请将其用作类型。 再次从您的代码来看,jquery不太可能将数据的二进制数组传递给您的方法。

我怀疑您需要Silverlight或Flash之类的东西来上传图像数据。 html沙箱在将文件数据发送到服务器时非常困难。

更新

您需要解析字符串。 您拥有所需的一切,这很好。 您需要查找base64字符串的起始位置。 使用该base64字符串并使用Convert.FromBase64String,它将为您提供一个作为图像的字节数组。 将此存储在您的数据库中。

我会将您的网络方法更改为接受字符串,因为这是画布生成的。 所有人都应该工作。

暂无
暂无

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

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