Usha Raj

Usha Raj

  • NA
  • 144
  • 57.4k

Upload Image in mvc

May 22 2015 7:32 AM
I need to upload the image ...,but when ever  I click on button my controller is not hitting....I don't know what is going wrong
here is my code ..can any one help me
 
Controller:
 
[HttpGet]
public ActionResult Upload(FormCollection col, HttpPostedFileBase file)
{
// DatContext db = new DatContext();
User userDetails = new User();
if (file != null)
{
var ext = Path.GetExtension(file.FileName).ToLower();
//var fileName = Path.GetFullPath(fileimg.FileName);
if (ext == ".png" || ext == ".jpg")
{
var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var random_code = new Random();
var code = new string(
Enumerable.Repeat(chars, 4)
.Select(s => s[random_code.Next(s.Length)])
.ToArray());
string fName = (file.FileName);
string filename = (file.FileName);
filename = code + filename;
string fPath = Path.Combine(Server.MapPath("~/UserImages/"));
string path = Path.Combine(Server.MapPath("~/UserImages"), filename);
file.SaveAs(path);
var image = Image.FromFile(Server.MapPath("~/UserImages/") + filename);
int maxWidth = 90;
int maxHeight = 90;
var ratioX = (double)maxWidth / image.Width;
var ratioY = (double)maxHeight / image.Height;
var ratio = Math.Min(ratioX, ratioY);
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio);
var newImage = new Bitmap(newWidth, newHeight);
Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
newImage.Save(fPath + fName);
userDetails.ProfilePicture = "<img style='width:35px;height:35px;border-radius:50px' src='../UserImages/" + fName + "'>";
}
}
return View(userDetails);
}
 
View:
@using (Html.BeginForm("Upload", "UserHome", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="oneInfo" style="width: 200px;">
<span class="spanForFileInput">
<input type="button" class="selectImage" value="Upload Image"/>
<input type="file" value="Upload Image" id="imgInp"/>
</span>
</div>
}
It's very urgent 
 
 

Answers (1)