Uploading Downloading PDF Files in ASP - NET MVC Using SQL Server
Uploading Downloading PDF Files in ASP - NET MVC Using SQL Server
cs
=======================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using Dapper;
using System.Data;
using System.Web.Mvc;
namespace MvcApplication5.Models
{
public class DataLayer
{
SqlConnection Con;
string s_Con;
public DataLayer()
{
Connectin();
}
private void Connectin()
{
s_Con = ConfigurationManager.ConnectionStrings["Db"].ToString();
Con = new SqlConnection(s_Con);
if (Con.State == System.Data.ConnectionState.Open)
{
Con.Close();
}
Con.Open();
}
public List<EmployeeModel> GetFileList()
{
List<EmployeeModel> DetList = new List<EmployeeModel>();
DetList = SqlMapper.Query<EmployeeModel>(Con, "GetFileDetails",
commandType: CommandType.StoredProcedure).ToList();
return DetList;
}
public bool SaveFileDetails(EmployeeModel objDet)
{
try
{
DynamicParameters Parm = new DynamicParameters();
Parm.Add("@FileName", objDet.FileName);
Parm.Add("@FileContent", objDet.FileContent);
Con.Execute("AddFileDetails", Parm, commandType:
System.Data.CommandType.StoredProcedure);
return true;
}
catch (Exception E) { return false; }
}
}
}
=================
EmployeeModel.cs
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication5.Models
{
public class EmployeeModel
{
public int ID { get; set; }
public string FileName { get; set; }
public byte []FileContent { get; set; }
public HttpPostedFileBase Files { get; set; }
}
}
===================
HomeController.cs
======================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication5.Models;
using Dapper;
using System.IO;
namespace MvcApplication5.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new DataLayer().GetFileList());
}
public ActionResult Upload()
{
return View();
}
[HttpPost]
public string Upload(EmployeeModel E)
{
String FileExt = Path.GetExtension(E.Files.FileName).ToUpper();
if (FileExt == ".PDF")
{
Byte[] data = new byte[E.Files.ContentLength];
E.Files.InputStream.Read(data, 0, E.Files.ContentLength);
E.FileName = E.Files.FileName; ;
E.FileContent = data;
if (new DataLayer().SaveFileDetails(E))
{
return string.Format("<script>alert('File
Uploaded');location.assign('/Home/Index');</script>");
}
else
{
return string.Format("<script>alert('Error
Occured');location.assign('/Home/Index');</script>");
}
}
else
{
return string.Format("<script>alert('Invalid File
');location.assign('/Home/Index');</script>");
}
}
[HttpGet]
public FileResult DownLoadFile(int id)
{
List<EmployeeModel> ObjFiles = new DataLayer().GetFileList();
var FileById = (from FC in ObjFiles
where FC.ID.Equals(id)
select new { FC.FileName,
FC.FileContent }).ToList().FirstOrDefault();
return File(FileById.FileContent, "application/pdf",
FileById.FileName);
}
}
}
================
Index.cshtml
@model IEnumerable<MvcApplication5.Models.EmployeeModel>
<th class="col-md-2"></th>
</tr>
<td>
@Html.ActionLink("Downlaod", "DownLoadFile", new { id=item.ID })
</td>
</tr>
}
</table>
=================
Upload.cshtml
@model MvcApplication5.Models.EmployeeModel
@{
ViewBag.Title = "www.compilemode.com";
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<div class="form-group">
<div class="col-md-8">
@Html.Action("Index", "Home")
</div>
</div>
</div>
}
===connection
<connectionStrings>
<add name="Db" connectionString="Data Source=192.168.0.200\omninet;Initial
Catalog=Dhananjay;User Id=sa;Password=sa@123*"
providerName="System.Data.SqlClient"/>
</connectionStrings>