我提前为这个noobish问题道歉。我对网络开发很陌生。
基本上,我的用户可以拥有个人资料照片。当用户编辑其个人资料图片时,它将保存到〜\ App_Data \ Profile_Pictures {user id}。{extension}。这很有效。我将配置文件图片url字符串重新格式化为客户端,因此它使用正斜杠。这也很有效。
在视图的图片标记中:
<img src=@Model.ProfilePictureUrl id="profileImage" />
Inspecting the HTML, the src attribute is correct, but the image doesn't load.
我将粘贴所涉及的代码段,但我认为解决方案不需要。对不起,这个令人畏缩的代码。只是想让它运转起来:
[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);
}
非常感谢任何帮助。