반응형
면도기에서 "Html.BeginForm"을 작성하는 방법
내가 이렇게 쓰면 :
form action = "Images"method = "post"enctype = "multipart / form-data"
효과가있다.
그러나 '@'가있는 Razor에서는 작동하지 않습니다. 내가 실수를 했습니까?
@using (Html.BeginForm("Upload", "Upload", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
Select a file <input type="file" name="file" />
<input type="submit" value="Upload" />
</fieldset>
}
내 컨트롤러는 다음과 같습니다
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload()
{
foreach (string file in Request.Files)
{
var uploadedFile = Request.Files[file];
uploadedFile.SaveAs(Server.MapPath("~/content/pics") +
Path.GetFileName(uploadedFile.FileName));
}
return RedirectToAction ("Upload");
}
다음 코드는 정상적으로 작동합니다.
@using (Html.BeginForm("Upload", "Upload", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
Select a file <input type="file" name="file" />
<input type="submit" value="Upload" />
</fieldset>
}
예상대로 생성합니다.
<form action="/Upload/Upload" enctype="multipart/form-data" method="post">
<fieldset>
Select a file <input type="file" name="file" />
<input type="submit" value="Upload" />
</fieldset>
</form>
반면에 당신은 같은 같은 다른 서버 측 구조의 컨텍스트 내부에이 코드를 작성하는 경우 if
또는 foreach
당신은 제거해야합니다 @
전과 using
. 예를 들면 다음과 같습니다.
@if (SomeCondition)
{
using (Html.BeginForm("Upload", "Upload", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
Select a file <input type="file" name="file" />
<input type="submit" value="Upload" />
</fieldset>
}
}
서버 측 코드에 관한 한 진행 방법은 다음과 같습니다.
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/content/pics"), fileName);
file.SaveAs(path);
}
return RedirectToAction("Upload");
}
참고 URL : https://stackoverflow.com/questions/8356506/how-to-write-html-beginform-in-razor
반응형
'Programing' 카테고리의 다른 글
Firefox 웹 콘솔이 비활성화 되었습니까? (0) | 2020.07.01 |
---|---|
메모장이 모두를 능가합니까? (0) | 2020.07.01 |
AppDomain이란 무엇입니까? (0) | 2020.07.01 |
IIS8의 IIS_IUSRS 및 IUSR 권한 (0) | 2020.07.01 |
동시 해시 세트 (0) | 2020.07.01 |