0% found this document useful (0 votes)
223 views4 pages

Uploading Downloading PDF Files in ASP - NET MVC Using SQL Server

This document contains code for an MVC application that allows uploading and downloading of PDF files. It includes classes for a data layer to connect to a SQL database and perform CRUD operations. Controllers handle the UI and call the data layer methods. Views display upload/download forms and list of files. The application uses Dapper for database access and stores file contents in a SQL table along with file metadata.

Uploaded by

hsuyip
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
223 views4 pages

Uploading Downloading PDF Files in ASP - NET MVC Using SQL Server

This document contains code for an MVC application that allows uploading and downloading of PDF files. It includes classes for a data layer to connect to a SQL database and perform CRUD operations. Controllers handle the UI and call the data layer methods. Views display upload/download forms and list of files. The application uses Dapper for database access and stores file contents in a SQL table along with file metadata.

Uploaded by

hsuyip
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 4

DataLayer.

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>

<table class="table table-bordered">


<tr>
<th class="col-md-4">
@Html.DisplayNameFor(model => model.FileName)
</th>

<th class="col-md-2"></th>
</tr>

@foreach (var item in Model) {


<tr>
<td>
@Html.DisplayFor(modelItem => item.FileName)
</td>

<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>

@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new {enctype =


"multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
<div class="form-group">
@Html.LabelFor(model => model.Files)
<div class="col-md-10">
@Html.TextBoxFor(model => model.Files, new{ @type = "file",
@multiple = "multiple" })
@Html.ValidationMessageFor(model => model.Files)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Upload" class="btn btn-primary" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10 text-success">
@ViewBag.FileStatus
</div>
</div>

<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>

You might also like