Modelinize bir byte[]
ve görünüm modelinizde bir HttpPostedFileBase
kullanabilirsiniz. Sonra
public class MyViewModel
{
[Required]
public HttpPostedFileBase File { get; set; }
}
ve: Örneğin:
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");
}
}
ve nihayet görünümünde:
@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>
}
Hi, bu harika, ancak mutlak çaylak olarak, burada iyi bir yer olurdu dosyaları saklamak için, örneğin, yalnızca site yöneticisinin kullanıcıların yükleyebileceği dosyaları (uygulama .exe dosyası) yüklemesine izin vermek istiyorum. – MoonKnight