Tag Archives: asp

How to create Picture Upload by using ASP MVC 4

This is a very simply example for how to using asp MVC. In this example doesn’t involved with your database. So start from create a model here is your code for your picture upload model. model is cs file, so save below code as PictureModel.cs

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace MvcDemo.Models
    public class PictureModel
        public HttpPostedFileBase PictureFile { get; set; }

Then create a action event to your Controller. Controller also just cs file, so save below to one of the Controllers file

//by default, this will be called by get 
public ActionResult PictureUpload()
            return View();

here is the view file code. view is the PictureUpload.cshtml

@model MvcDemo.Models.PictureModel
    ViewBag.Title = "Picture Upload";

Picture Upload

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = “multipart/form-data” })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true)


@Html.LabelFor(model => model.PictureFile)


@Html.TextBoxFor(model => model.PictureFile, new { type = “file” }) @Html.ValidationMessageFor(model => model.PictureFile)

} @section Scripts { @Scripts.Render(“~/bundles/jqueryval”) }

After submit, it will call the post, below is the action event for post

        public ActionResult PictureUpload(PictureModel model)
            if (model.PictureFile.ContentLength > 0)
                var fileName = Path.GetFileName(model.PictureFile.FileName);
                var filePath = Server.MapPath("~/Content/Uploads");
                string savedFileName = Path.Combine(filePath, fileName);
            return View(model);

ASP.NET Page Life Cycle

  • Page request A request to an ASPX page starts the life cycle of that page. When the web server is able and allowed to return a cached copy of the page, the entire life cycle is not executed. In all other situations, the page enters the start phase.
  • Start In this phase, the page gets access to properties like Request and Response that are used to interact with the page’s environment. In addition,during this phase the PreInit event is raised to signal that the page is about to go into the initialization phase.
  • Page initialization During this phase, the controls you have set up in your page or added programmatically become available. Additionally, the Page class fires three events: Init, InitComplete, and PreLoad. Also during this phase, the control properties are loaded from View State and Control State again during a postback. So, for example, when you change the selected item in a DropDownList and then cause a postback, this is the moment where the correct item gets preselected in the drop-down list again, which you can then work with in your server-side code.
  • Load During this phase the page raises the Load event.
  • Validation In the validation phase, the Validation controls used to validate user input are processed.
  • Postback event handling During this phase, the controls in your page may raise their own events. For example, the DropDownList may raise a SelectedIndexChanged event when the user has chosen a different option in the list. Similarly, a TextBox may raise the TextChanged event when the user has changed the text before she posted back to the server. When all event processing is done, the page raises the LoadComplete event. During this phase the PreRender event is raised to signal that the page is about to render to the browser. Shortly after that, SaveStateComplete is raised to indicate that the page is done storing all the relevant data for the controls in View State.
  • Rendering Rendering is the phase where the controls (and the page itself) output their HTML to the browser.
  • Unload The unload phase is really a clean-up phase. This is the moment where the page and controls can release resources like database connections.During this phase, the Unload event is raised so you can handle any cleanup you may need to do.