Я использую ASP.Net MVC 5, и я хочу создать аватар для моих профилей пользователей. Я не уверен, что то, что я делаю до сих пор, это правильный путь, особенно по соображениям безопасности, поэтому я хотел получить некоторые советы.
Что я делаю до сих пор
В представлении:
@using (Html.BeginForm("ManageUser", "Account", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="Add Avatar" />
}
В контроллере:
internal static bool SaveAvatar(User user, HttpPostedFileBase file)
{
if (file == null || file.ContentLength <= 0 || file.ContentLength > MAX_LENGTH)
return false;
//I think I should convert the file somehow instead of saving it
var path = HostingEnvironment.MapPath("~/img/avatar/") + string.Format("{0}.png", user.UserName);
file.SaveAs(path);
user.HasAvatar = true;
return true;
}
У меня есть несколько проблем:
- В приведенном выше коде, как я прокомментировал, я думаю, вместо того, чтобы просто сохранить какой пользователь отправил мне, я должен каким-то образом использовать библиотеку для преобразования изображение в файл PNG и сохраните его. Если да, есть ли для этого хорошая и простая библиотека?
- Интересно, будет ли полезно использовать имя пользователя в качестве файла. В конце концов, они выбирают это имя, и это не то, что я решаю.
- Хорошо ли использовать простые изображения или я должен создать контроллер для проверки запросов или маршрутизации запросов на скрытое изображение?
Если то, что я делаю, это TOTALLY неправильно, и вы знаете лучший источник, чтобы узнать правильный путь, пожалуйста, укажите мне в правильном направлении. Спасибо.