Hello
I have an
element in my .html file. When user select his/her image, I get the selected image using JavaScript and send it to controller as JSON:
var _Data = { Image = $("#ImageFileUpload").val();
and in my controller, I save it in my Data Base:
byte[] binData;
byte[] byteArrey = System.Text.Encoding.UTF8.GetBytes(_LawyerArgument.Picture);
MemoryStream _Stream = new MemoryStream(byteArrey);
UserPicture _UserPicture = _LawEntities.UserPicture
.Where(c => c.UserId == _UserID).SingleOrDefault();
if (_UserPicture != null)
{
using (var _BinaryReader = new BinaryReader(_Stream))
{
binData = _BinaryReader.ReadBytes(Int32.Parse(_Stream.Length.ToString()));
_BinaryReader.Close();
}
_UserPicture.picture = binData;
}
else
{
_UserPicture = new UserPicture();
using (var _BinaryReader = new BinaryReader(_Stream))
{
binData = _BinaryReader.ReadBytes(Int32.Parse(_Stream.Length.ToString()));
_BinaryReader.Close();
}
_UserPicture.picture = binData;
_LawEntities.UserPicture.Add(_UserPicture);
}
_LawEntities.SaveChanges();
it stores record in my table and I can see binary data as image, but I think I am wrong in Insert OR Fetch data. because when I want to show selected image to user in web site, it does not show the image. I have no error in debug or run-time.
I retrieve image by these codes:
var _LawyerPictures = _Law.UserPicture
.Take(4)
.Select(s => new
{
Picture = s.picture,
ID = s.UserId
}).ToList();
foreach (var item in _LawyerPictures)
{
string _Path = Path.Combine(Directory.GetCurrentDirectory(), @"/TemporaryUserPictures/");
if (!Directory.Exists(_Path))
{
Directory.CreateDirectory(_Path);
var path = HttpContext.Current.Server.MapPath(_Path + item.ID + ".png");
File.WriteAllBytes(path, item.Picture);
}
}
I save images to a temporary folder and then use it to show them.
What I have tried:
I have tried codes above, but I do not know where I have the problem, In storing image, or in retrieve.
thanks in Advance