Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
[HttpPost]
public ActionResult Registration(FormCollection obj_frmRegistration)
{
    objc.AdminName = obj_frmRegistration["AdminName"];
    objc.Gender = obj_frmRegistration["Gender"];
    objc.ImageUrl = obj_frmRegistration["Image"];

    using (var stream = new MemoryStream())
    {
        objc.ImageUpload.InputStream.CopyTo(stream);
        byte[] photo = stream.ToArray();
    }


after converting that image it should store in
objc.profileimage.

profile image is of type byte
Posted
Updated 19-Nov-15 23:52pm
v2

1 solution

This is not a good way to get the image from the view. Use HttpPostedFileBase[^].
An example would be-
Controller:
C#
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload(PhotoForSingleItem photo)
{
  PhotoViewImage newImage = new PhotoViewImage();
  HttpPostedFileBase file = Request.Files["OriginalLocation"];
  newImage.Name = photo.Name;
  newImage.Alt = photo.AlternateText;
  newImage.ContentType = file.ContentType;

  Int32 length = file.ContentLength;
  byte[] tempImage = new byte[length];
  file.InputStream.Read(tempImage, 0, length);
  newImage.ActualImage = tempImage ;

  newImage.Save();

  return View();
}

View:
HTML
<form method="post" enctype="multipart/form-data" action="Photo/Upload">
  <div>
    <span>
     Name:
   </span>
   <span>
     <input type="text" id="Name" name="Name" />
   </span>
  </div>
  <div>
    <span>
      Alternate Text:
    </span>
    <span>
     <input type="text" id="AlternateText" name="AlternateText" />
    </span>
  </div>
  <div>
    <span>
      Image
    </span>
    <span>
      <input type="file" id="OriginalLocation" name="OriginalLocation" />
    </span>
  </div>
  <div>
    <input type="submit" value="Upload" />
  </div>
</form>


See the complete tutorial here-
ASP.Net MVC: Upload Image to Database and Show Image “Dynamically” Using a View[^]

-KR
 
Share this answer
 
Comments
Member 11970398 20-Nov-15 6:26am    
Sorry ,Can u Please correct my code ,i am unable to do so
Member 11970398 20-Nov-15 6:36am    
i have tried like

HttpPostedFileBase file = Request.Files["file"];
objc.ContentType = file.ContentType;
Int32 length = file.ContentLength;
byte[] tempImage = new byte[length];
file.InputStream.Read(tempImage, 0, length);
objc.ProfileImage = tempImage;
but showing object reference null
Krunal Rohit 20-Nov-15 10:50am    
Did you try to debug your code ?

-KR
Member 11970398 23-Nov-15 1:06am    
plz help me
Member 11970398 23-Nov-15 1:00am    
yes nothing happen

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