Wednesday 1 January 2020

Working with File Upload Control using Asp.Net MVC – PART - IV

Agenda :
            Dear all In this article I would like to demonstrate Working with File Upload Control using Asp.Net MVC – PART - IV

Description :
          In previous articles, I explained on How to work with File upload control using Asp.Net – PART - IWorking with File Upload control using Asp.Net – PART - II,
How to resolve maximum requested length exceeded error with file upload control – PART - III
 different articles related to C#.Net,Asp.NetSQL ServerXML, and DotNetInterviewQuestions Related articles. Now Let's work on Working with File Upload Control using Asp.Net MVC – PART - IV



File upload Working Steps Using Asp.Net MVC :

  • In the course of achieving file upload control using Asp.Net  MVC only two entities will communicate with each other i.e.., View and Controller.
  • User Clicks on FileUpload control and uploads the certain file
  • When the user clicks on the upload / submit button it hits the respective POST method and sends the request to the server
  • ASP.Net MVC caches the data in server memory or to disk space depending upon the size of the file
  • Asp.Net MVC defines the controller and appropriate action method that will handle the request
  • Asp.Net MVC handles the request through the controller with the help of request property i.e..., Controller. The request which receives HttpPostedFilesBase for the current request.
  • ASP.Net MVC  sends a response to the client through Controller.Response




View Code (Design Code) :
@{
    ViewBag.Title = "File Upload Example using Asp.Net MVC By PNV Technical Hub";
}
@using (Html.BeginForm("FileUpload"
                        "Home"
                        FormMethod.Post, 
                        new { enctype = "multipart/form-data" })) 
{                   
    <label for="file">Upload File:</label> 
    <input type="file" name="fileUp" id="fileUp"/><br><br> 
    <input type="submit" value="Upload File"/> 
    <br><br> 
    <span style="font-weight:bold;color:red;">@ViewBag.Message  </span>   
} 

Controller Code (Server Side Code) :


public ActionResult FileUpload()
        {
         ViewBag.Message = "File Upload Example using Asp.Net MVC By PNV Technical Hub";
            return View();
        }
        [HttpPost]
        public ActionResult FileUpload(HttpPostedFileBase fileUp)
        {
            if (fileUp != null && fileUp.ContentLength > 0)
            {
                try
                {
                    string path = Path.Combine(Server.MapPath("~/Files"),
                                               Path.GetFileName(fileUp.FileName));
                    fileUp.SaveAs(path);
                    ViewBag.Message = "File uploaded successfully";
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "Error Occurred:" + ex.Message.ToString();
                }
            }
            else
            {
                ViewBag.Message = "Select a file to upload.";
            }
            return View();
        }


Code Explanation :
  • The Html.BeginForm method creates HTML form with all necessary control which is required to represent in the form
  • Html.BeginForm method accepts four parameters 1.ActionMethodName 2.ControllerName  3.MethodType 4.Attributes
  • In this context FileUpload is an Action Name, Home is a Controller Name, POST is the method type, multipart/form-data is the enctype attribute
  • The Form method is POST, and the form encoding type is multipart/form-data. These parameters are essential for uploading binary data to the server.
  • The input element type=“file” is a file upload control required in order to upload a file
  • The name which is specified in input type=“file” control element identifies the upload file in the HttpPostedFileBase object
  • So that HttpPostedFileBase  object process the file in the controller according to the requirement
Steps to Implement :
  • Go To Visual Studio -->Click on New Project
  • click on -->Web Template
  • click on -->ASP.NET MVC4 Web Application
  • Implement the code as discussed



No comments :

Post a Comment