Я написал Javascript, который захватывает изображения с веб-камеры, а затем я хотел бы сериализовать его в свою базу данных SQL Server.Поле в базе данных является обычным varbinary (max).Из firebug я вижу, что raw image передается моему веб-методу, но он продолжает выдавать следующую ошибку:
"Ошибка вставки 797719006-: Telerik.OpenAccess.RT.sql.SQLException: Невозможно вставить значение NULL в столбец 'Picture', таблица 'GoldStar.Students.Pictures';
JavaScript, который захватывает изображение, определенно работает очень хорошо, поэтому я знаю, что там нет проблем, это просто вопрос сохранения его в базе данных. Возможно, я слишком усложняю ситуацию, обновляя веб-метод через ajax ???
Я создал веб-метод с включенной регистрациейстраницы, и это выглядит так:
[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)
}
});
}