My apologies in advance for the noobish question. I am very new to web development.
Basically, My users can have a profile picture. When the user edits their profile picture, it will be saved to ~\App_Data\Profile_Pictures{user id}.{extension}. This is working great. I reformat the profile picture url string when returning it to the client, so it uses forward slashes. This is working well too.
In the image tag of the view:
<img src=@Model.ProfilePictureUrl id="profileImage" />
Inspecting the HTML, the src attribute is correct, but the image doesn't load.
I will paste the involved code segments, although I don't think it's necessary for the solution. Sorry for the cringe-worthy code. Just trying to get it working:
[Authorize]
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
private readonly IDataRepository _dataRepository;
private readonly string _profilePicDir = ConfigurationManager.AppSettings["ProfilePicSaveDirectory"];
private readonly string _defaultPic = ConfigurationManager.AppSettings["DefaultProfilePicLocation"];
public AccountController(IDataRepository repo)
{
_dataRepository = repo;
}
[HttpGet]
public ActionResult EditProfile()
{
var userId = User.Identity.GetUserId();
var user = _dataRepository.GetApplicationUserById(userId);
ProfileViewModel model = new ProfileViewModel()
{
ProfilePictureUrl = user.ProfilePictureUrl.Replace(@"\", "/"),
Biography = user.Biography
};
return View(model);
}
[HttpPost]
public ActionResult EditProfile(ProfileViewModel model)
{
if (ModelState.IsValid)
{
// Retrieve current user
var userId = User.Identity.GetUserId();
var user = _dataRepository.GetApplicationUserById(userId);
//If it isn't the single-instance default picture, delete the current profile
// picture from the Profile_Pictures folder
if (!String.Equals(user.ProfilePictureUrl, _defaultPic))
System.IO.File.Delete(Server.MapPath(user.ProfilePictureUrl));
// Create a profile picture URL to save to.
// This will map to App_data\Profile_Pictures\{User ID}.{File Extension}
// Set the new file name to the current user's ID
var profilePicUrl = _profilePicDir + userId +
"." + BcHelper.GetFileExtension(model.ProfilePicture);
// Save the profile picture and update the user's
// ProfilePicUrl property in database
model.ProfilePicture.SaveAs(Server.MapPath(profilePicUrl));
user.ProfilePictureUrl = profilePicUrl.Remove(0,1).Replace(@"\", "/");
// Save the user's biography
user.Biography = model.Biography;
_dataRepository.UpdateProfile(user);
return RedirectToAction("Index", "Groups");
}
return View(model);
}
Any help would be greatly appreciated.