이 뭐가 문제 :의 asp.net MVC 3 - 서버에 파일을 업로드 나는 데이터베이스 모델에 파일 이름을 저장할 수 없습니다
보기
@model GestionWeb.DAL.Client
@using (Html.BeginForm("Index", "Config", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.LabelFor(model => model.TimeZone)
@Html.EditorFor(model => model.TimeZone)
@Html.ValidationMessageFor(model => model.TimeZone)
@Html.HiddenFor(model => model.ClientId)
@Html.LabelFor(model => model.Logo)
<input type="file" name="Logo" id="Logo" />
@Html.ValidationMessageFor(model => model.Logo)
<input type="submit" value="Upload" />
}
컨트롤러 :
[HttpPost]
public ActionResult Index(Client client)
{
if (ModelState.IsValid)
{
if (Request.Files[0].ContentLength > 0)
{
HttpPostedFileBase file = Request.Files[0];
string filePath = Path.Combine(HttpContext.Server.MapPath("/Upload/"), Path.GetFileName(file.FileName));
file.SaveAs(filePath);
client.Logo = file.FileName;
}
db.Client.Attach(client);
UpdateModel<Client>(client);
//db.ObjectStateManager.ChangeObjectState(client, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(client);
}
파일을 쓸 수는 있지만 파일 이름은 데이터베이스에 저장되지 않고 오류가 발생하지 않으며 TimeZone 필드 (및 기타 많은 항목)를 업데이트 할 수 있습니다.
내가 뭘 잘못하고있어? 나는 알아낼 수 없다!
감사합니다.
되지 않음 대답하지만,이를 참조하십시오 - http://stackoverflow.com/questions/4535627/where-is-the-best-place-to-save-images-from-users -upload/4535684 # 4535684 그리고 사용자가 저장 한 파일에 대해 사용자가 제공 한 파일 이름을 사용하여 보안이 중단된다면 걱정할 필요가 없습니다. –
고맙습니다. 위험을 감수 하겠지만, 그 페이지는 제 문제를 해결하지 못합니다. – Santiago