c# mvc file upload .net4.5


using System.IO;
using System.Web;
using System.Web.Mvc;


namespace MvcFileUpload1.Controllers
{
    public class UploadController : Controller
    {
     
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult UploadFile()
        {
            return View();
        }
        [HttpPost]
        public ActionResult UploadFile(HttpPostedFileBase file)
        {
            try
            {
                if (file.ContentLength > 0)
                {
                    string _FileName = Path.GetFileName(file.FileName);
                    string _path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName);
                    file.SaveAs(_path);
                }
                ViewBag.Message = "File Uploaded Successfully!!";
                return View();
            }
            catch
            {
                ViewBag.Message = "File upload failed!!";
                return View();
            }
        }
    }
}


********** VİEW ***********************


@{
    ViewBag.Title = "UploadFile";
}


<h2>UploadFile</h2>

@using (Html.BeginForm("UploadFile", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    <div>
        @Html.TextBox("file", "", new { type = "file" }) <br />

        <input type="submit" value="Upload" />

        @ViewBag.Message

    </div>


}


//********* GitHub public 
https://github.com/doktoralban/MvcFileUpload1


Yorumlar