ASP. net mvc uploads files, asp. netmvc uploads files
I recently learned how to upload files using ASP. NET MVC by referring to network documents. It is the most basic and does not use jQuery or other technologies.
1. Define Model
Public class TestModel
{
[Display (Name = "title")]
[Required]
Public string Title
{
Get;
Set;
}
[Display (Name = "content")]
[Required]
[DataType (DataType. MultilineText)]
Public string Content
{
Get;
Set;
}
Public string AttachmentPath
{
Get;
Set;
}
}
2. Controller
Public class TestController: Controller {// GET:/Test/public ActionResult Upload () {return View ();} /// <summary> /// method of submission /// </summary> /// <param name = "tm"> model data </param> /// <param name = "file"> the object to be uploaded, the parameter name here must be the same as the Upload tag name in View </param> // <returns> </returns> [HttpPost] public ActionResult Upload (TestModel tm, HttpPostedFileBase file) {if (file = null) {return Content ("no file! "," Text/plain ");} var fileName = Path. Combine (Request. MapPath ("~ /Upload "), Path. getFileName (file. fileName); try {file. saveAs (fileName); // tm. attachmentPath = fileName; // obtain all model information tm. attachmentPath = ".. /upload/"+ Path. getFileName (file. fileName); // return Content ("uploaded successfully! "," Text/plain "); return RedirectToAction (" Show ", tm);} catch {return Content (" Upload exception! "," Text/plain ") ;}} public ActionResult Show (TestModel tm) {return View (tm );}}
3. View
3.1 Upload. cshtml
@ Model MvcApplication1.Models. TestModel @ {ViewBag. Title = "Upload" ;}<! DOCTYPE html>
3.2 Show. cshtml
@ Model MvcApplication1.Models. testModel @ {ViewBag. title = "Show" ;}< h2> Show
Asp net MVC3 File Upload Problems
Consider the following code: Create First: create a form
@ Using (Html. beginForm ("Index", "Home", FormMethod. post, new {enctype = "multipart/form-data "})) {<input type = "file" name = "file"/> <input type = "submit" value = "OK"/>}
2. Create controlle
Public class HomeController: Controller {// This action renders the form public ActionResult Index () {return View ();} // This action handles the form POST and the upload [HttpPost] public ActionResult Index (HttpPostedFileBase file) {// Verify that the user selected a file if (file! = Null & file. ContentLength> 0) {// extract only the fielname var fileName = Path. GetFileName (file. FileName); // store the file inside ~ /App_Data/uploads folder var path = Path. Combine (Server. MapPath ("~ /App_Data/uploads "), fileName); file. saveAs (path);} // redirect back to the index action to show the form once again return RedirectToAction ("Index ");}}
A copy of ASPNET MVC3 file upload code is required
View:
@{
ViewBag. Title = "DownloadAdd ";
}
<Html>
<Head>
<Title> background </title>
</Head>
<Body>
<Div id = "body-wrapper">
@ RenderPage ("../Shared/BackLeft. cshtml ")
<Form action = "DownloadAdd" method = "post" enctype = "multipart/form-data">
<Div id = "main-content">
<! -- Main Content Section with everything -->
<Noscript>
<! -- Show a notification if the user has disabled javascript -->
<Div class = "notification error png_bg">
<Div>
Javascript is disabled or is not supported by your browser. Please <a href = "browsehappy.com /"
Title = "Upgrade to a better browser"> upgrade </a> your browser or <a href = "www.google.com/..=23852"
Title = "Enable Javascript in your browser"> enable </a> Javascript to navigate
The interface properly. Download From <a href = "...... the remaining full text>