Wednesday 7 August 2013

Uploading and returning files in ASP.NET MVC





Description:-
                        In this Example we explain that How to Upload File or Image in MVC.in previous we Explain that how to upload image in Asp.Net using File Upload Control.

here we upload file in SqlServer Database at a time only one file and we use the SaveDta data method to upload file in the Database and in this method we pass two Argument that are SingleFileUploadModel containers, HttpPostedFileBase file.

 here SingleFileUploadModel are contain the File that user are upload and HttpPostedFileBase are used to check the name of file and size of file or type of the File.

what is use of  [HttpPost] :-



The POST request method is used to request that a web server accepts the data enclosed in the request message's body for storage
Some important point about data submitted using HttpPost
A Submit button will always initiate an HttpPost request.
Data is submitted in http request body.
Using Post Data is not visible in the url when form is Submited.
It is more secured but slower as compared to GET.
It use heap method for passing form variable
It can post unlimited form variables or data.

To show Example of How to Upload Image or File in Asp.Net Click Here Upload File and Bind to Gridview in Asp.Net


First You have to Create a Class file in Model Folder with the name

SingleFileUploadModel





SingleFileUploadModel.cs:-




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

namespace MVCFileUpload.Models
{
    public class SingleFileUploadModel
    {
        public string MyProperty1 { get; set; }
        public string MyProperty2 { get; set; }
        public string MyProperty3 { get; set; }
        public string FileName { get; set; }
    }
}


Now Add Controller in Controller Folder with the name


SingleFileUploadController

SingleFileUploadController.cs:-
 




using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using MVCFileUpload.Models;

using System.IO;



namespace MVCFileUpload.Controllers

{

    public class SingleFileUploadController : Controller

    {
        public ActionResult Index()
        {
            return View();
        }
       
        [HttpPost]
        public ActionResult SaveData(SingleFileUploadModel containers, HttpPostedFileBase file)
        {
            string fileName = string.Empty; ;
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                fileName = Path.GetFileName(file.FileName).ToString();
                // store the file12 inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath("~/Uploads"), fileName);
                file.SaveAs(path);
            }
            // add file12 name to model and save model
            containers.FileName = fileName;
            //Save model
            ViewData["message"] = "Uploaded Successfully";
            return View("index");
        }
    }
}

 

now add the Index.cshtml File or view in the folder of SingleFileUpload.

Index.cshtml:-

@model MVCFileUpload.Models.SingleFileUploadModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}



<style>
    div{padding:5px;}
</style>
<h2>Index</h2>
@ViewData["message"]
@using (Html.BeginForm("SaveData", "SingleFileUpload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
    @Html.LabelFor(x => x.MyProperty1)
    @Html.EditorFor(x => x.MyProperty1) </div>
    <div>
    @Html.LabelFor(x => x.MyProperty1)
    @Html.EditorFor(x => x.MyProperty2)</div>
    <div>
    @Html.LabelFor(x => x.MyProperty1)
    @Html.EditorFor(x => x.MyProperty3)</div>
   
   
    <div><input type="file" name="file" id="file" /> </div>
    <input type="submit" value="submit" />
}

This entry was posted in :

0 comments:

Post a Comment