How to upload files to a server
<% using (Html.BeginForm("UploadFile", "AccessRequestProvider",
FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
Where UploadFile is the method and AccessRequestProvider is the controller (.cs)
<input type="file" name="files" id="file1" class="filestyle" data-classbutton="btn btn-primary" data-input="false" data-classicon="icon-plus" data-buttontext="TEst.">
<input type="file" name="files" id="file2" class="filestyle" data-buttonname="btn-primary" data-buttontext=" TEst.">
- In the ASPX
<% using (Html.BeginForm("UploadFile", "AccessRequestProvider",
FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
Where UploadFile is the method and AccessRequestProvider is the controller (.cs)
- Then include the fields to upload the files
<input type="file" name="files" id="file1" class="filestyle" data-classbutton="btn btn-primary" data-input="false" data-classicon="icon-plus" data-buttontext="TEst.">
<input type="file" name="files" id="file2" class="filestyle" data-buttonname="btn-primary" data-buttontext=" TEst.">
- Close the form
<%} %>
- In the Controller
public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> files)
{
foreach (var file in files)
{
if (file.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("~/Uploads"),
Path.GetFileName(file.FileName));
file.SaveAs(filePath);
}
}
return View("~/Views/AccessRequestProvider/UploadFile.aspx");
}
- In case you want only to upload one file
public ActionResult UploadFile(HttpPostedFileBase uploadFile)
{
if (uploadFile.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("~/Uploads"),
Path.GetFileName(uploadFile.FileName));
uploadFile.SaveAs(filePath);
}
return View("~/Views/AccessRequestProvider/UploadFile.aspx");
}
Comentarios
Publicar un comentario