MVC | Upload file
Upload file in mvc in local folder of web application.
HTML:
@using (Html.BeginForm("UploadFile", "HomeBanner", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
@Html.TextBox("file", "", new { type = "file" }) <br />
<input type="submit" value="Upload" />
@ViewBag.Message
</div>
}
Code:
using System; using System.IO; using System.Web; [HttpPost] public ActionResult UploadFile(HttpPostedFileBase file) { try { if (file.ContentLength > 0) { string _FileName = Path.GetFileName(file.FileName); string _path = Path.Combine(Server.MapPath("~/Temp"), _FileName); file.SaveAs(_path); } ViewBag.Message = "File Uploaded Successfully!!"; return View(); } catch(Exception ex) { ViewBag.Message = ex.Message; return View(); } }
No comments:
Post a Comment