모델에 byte[]
을, 뷰 모델에 HttpPostedFileBase
을 사용할 수 있습니다. 다음
public class MyViewModel
{
[Required]
public HttpPostedFileBase File { get; set; }
}
과 : 예를 들어
public class HomeController: Controller
{
public ActionResult Index()
{
var model = new MyViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
byte[] uploadedFile = new byte[model.File.InputStream.Length];
model.File.InputStream.Read(uploadedFile, 0, uploadedFile.Length);
// now you could pass the byte array to your model and store wherever
// you intended to store it
return Content("Thanks for uploading the file");
}
}
마지막으로보기에 :
@model MyViewModel
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
@Html.LabelFor(x => x.File)
@Html.TextBoxFor(x => x.File, new { type = "file" })
@Html.ValidationMessageFor(x => x.File)
</div>
<button type="submit">Upload</button>
}
안녕하세요, 멋진,하지만 절대 멍청한 놈으로, 어디에 가장 좋은 장소가 될 것입니다 예를 들어, 사이트 관리자가 사용자가 다운로드 할 수있는 파일 (응용 프로그램 .exe 파일)을 업로드하는 것을 허용하려는 경우 파일을 저장할 수 있습니까? – MoonKnight