Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello
I have an
HTML


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:
JavaScript
var _Data = { Image = $("#ImageFileUpload").val();

and in my controller, I save it in my Data Base:
C#
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:
C#
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
Posted
Updated 8-Dec-16 17:24pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900