私は自分のコントローラーを持っています
[HttpPost]
public ActionResult ChangeAvatar(HttpPostedFileBase file)
{
AvatarHelper.AvatarUpdate(file, User.Identity.Name);
return RedirectToAction("Index", "Profile");
}
そして、ファイルが jpeg/png 形式であるかどうかを既に確認しています。
private static bool IsImage(string contentType)
{
return AllowedFormats.Any(format => contentType.EndsWith(format,
StringComparison.OrdinalIgnoreCase));
}
public static List<string> AllowedFormats
{
get { return new List<string>() {".jpg", ".png", ".jpeg"}; }
}
必要なもの-アップロードされたファイルが実際の画像ファイルであり、画像拡張子を持つtxtファイルではないことを確認してください。
アップロードしたファイルを次のように変換します。
using (var image = System.Drawing.Image.FromStream(postedFile.InputStream))
{
///image stuff
}
入力ストリームから画像を作成するときに try/catch ブロックを考えていますが、それを行う良い方法はありますか? ありがとう)
PS
ファイルが実際の画像かどうかを確認する別の (try/catch ブロックのより効率的な方法) 方法があるのだろうか?