누구나 도와 주시면 감사하겠습니다. 보기에서 폼에 파일 컨트롤을 입력하고 누군가 그림을 선택하고 양식의 제출 단추를 클릭하면 해당 파일을 응용 프로그램의/Pictures 폴더에 저장해야하며 파일 경로는 SQL 데이터베이스에 저장해야합니다. 문자열 (예 :/Pictures/filename).ASP.NET MVC - 이미지를 업로드하고 데이터베이스에 URL을 저장하는 방법
모델 클래스 부분 :
[Table("Automobil")]
public partial class Automobil
{ .....
[Required]
[StringLength(30)]
public string Fotografija{ get; set; }
......
보기 (만들기) 파일 부분 :
@using (Html.BeginForm("Create", "Automobili", FormMethod.Post, new { enctype = "multipart/form-data" }))
....
<div class="form-group">
<div class="editor-field">
@Html.TextBoxFor(model => model.Fotografija, new { type = "file" })
@Html.ValidationMessageFor(model => model.Fotografija, "", new { @class = "text-danger" })
</div>
</div>
....
컨트롤러 부분 : 내가 그렇게 사진을 어떻게해야합니까 무엇
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "AutomobilID,Marka,Model,Godiste,Zapremina_motora,Snaga,Gorivo,Karoserija,Fotografija,Opis,Cena,Kontakt")] Automobil automobil)
{
if (ModelState.IsValid)
{
db.Automobils.Add(automobil);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(automobil);
}
(Fotografija)은 응용 프로그램 폴더의 Pictures 및 SQL Base의 파일 경로 (예 :/Pictures/filename)에 저장할 수 있습니까?
초보자를 미리 도와 주셔서 감사합니다.
데이터베이스에 전체 경로를 넣지 마십시오. 파일 이름을 넣으십시오. 이렇게하면 포함 된 폴더의 위치를 변경하거나 이름을 바꿀 때 데이터를 변경할 필요가 없습니다. 코드를 올바르게 구성했다고 가정하면 코드를 한 곳에서 변경하면됩니다. –
위 코드를 사용하면 사진에 최대 30 자의 유효성 검증 오류가 표시됩니다. 또한 응용 프로그램에 그림 자체를 저장하지 않습니다. – Oggie
제거 [[StringLength (30)]' – Oluwafemi