Friday, 25 April 2014

MVC

How to upload file and download file without giving any extension using MVC3?

1. Add new folder in solution and name like as UploadImages

2. Model ( Add DataClasses class)

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


namespace MvcApplication10.Models
{
    public class DataClasses
    {
        public List<FileNames> GetFiles()
        {
            List<FileNames> lstfile = new List<FileNames>();
            DirectoryInfo dr = new DirectoryInfo(HostingEnvironment.MapPath("~/UploadImages"));
            int i = 0;
            foreach (var item in dr.GetFiles())
            {
                lstfile.Add(new FileNames() { id = i + 1, name = item.Name, FilePath = dr.FullName + @"\" + item.Name });
                i = i + 1;
            }
            return lstfile;
        }
    }
    public class FileNames
    {
        public int id{get; set;}
        public string name{get;set;}
        public int marks{get;set;}
        public string FilePath { get; set; }
    }
}

  3. Home Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication10.Models;

namespace MvcApplication10.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        DataClasses objData;

        public HomeController()
        {
            objData = new DataClasses();
        }
        public ActionResult Index()
        {
            var files = objData.GetFiles();
            return View(files);
        }
        public FileResult Download(string id)
        {
            int fid = Convert.ToInt32(id);
            var files = objData.GetFiles();
            string filename = (from f in files
                               where f.id == fid
                               select f.FilePath).First();

            var ff = filename.Split('.');
            string ext = ff[1];
            string contentType = "application/" + ext;
            return File(filename, contentType, "Report" + "." + ext);
        }

        [HttpPost]
        public ActionResult Index(IEnumerable<HttpPostedFileBase> File)
        {
            string physicalpath = HttpContext.Server.MapPath("~") + "UploadImages" + "\\";
            for (int i = 0; i < Request.Files.Count; i++)
            {
                Request.Files[0].SaveAs(physicalpath + System.IO.Path.GetFileName(Request.Files[i].FileName));
            }
            var files = objData.GetFiles();
            return View(files);
            //return View();
          
        }

    }
}

  4.  Index Page

 @{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <fieldset>
    <legend>
    Upload Files
    </legend>
    <ol>
    <li><input type="file" id="fileUPload1" name="fileUpload1" /></li>
    </ol>
        <input type="submit" value="Upload" />
    </fieldset>
}
@model IEnumerable<MvcApplication10.Models.FileNames>
         
<table>
<tr><th>FileId</th>
<th>FileName<th>
</tr>


@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.id)
</td>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.ActionLink("Download", "Download", new { id = item.id })

</td>
</tr>
}
</table>

View alike:-

 


No comments:

Post a Comment

Thanks for comments.