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

ASP.NET MVC File Upload and Download

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
406 views4 pages

ASP.NET MVC File Upload and Download

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

DataLayer.

cs
=======================

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using Dapper;
using [Link];
using [Link];

namespace [Link]
{
public class DataLayer
{
SqlConnection Con;
string s_Con;
public DataLayer()
{
Connectin();
}
private void Connectin()
{
s_Con = [Link]["Db"].ToString();
Con = new SqlConnection(s_Con);
if ([Link] == [Link])
{
[Link]();
}
[Link]();
}
public List<EmployeeModel> GetFileList()
{
List<EmployeeModel> DetList = new List<EmployeeModel>();
DetList = [Link]<EmployeeModel>(Con, "GetFileDetails",
commandType: [Link]).ToList();
return DetList;
}
public bool SaveFileDetails(EmployeeModel objDet)
{
try
{
DynamicParameters Parm = new DynamicParameters();
[Link]("@FileName", [Link]);
[Link]("@FileContent", [Link]);
[Link]("AddFileDetails", Parm, commandType:
[Link]);
return true;
}
catch (Exception E) { return false; }
}
}
}

=================

[Link]
sing System;
using [Link];
using [Link];
using [Link];

namespace [Link]
{
public class EmployeeModel
{
public int ID { get; set; }
public string FileName { get; set; }
public byte []FileContent { get; set; }
public HttpPostedFileBase Files { get; set; }
}
}

===================
[Link]
======================

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using Dapper;
using [Link];
namespace [Link]
{
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 = [Link]([Link]).ToUpper();
if (FileExt == ".PDF")
{
Byte[] data = new byte[[Link]];
[Link](data, 0, [Link]);
[Link] = [Link]; ;
[Link] = data;
if (new DataLayer().SaveFileDetails(E))
{
return [Link]("<script>alert('File
Uploaded');[Link]('/Home/Index');</script>");
}
else
{
return [Link]("<script>alert('Error
Occured');[Link]('/Home/Index');</script>");
}
}
else
{
return [Link]("<script>alert('Invalid File
');[Link]('/Home/Index');</script>");
}
}
[HttpGet]
public FileResult DownLoadFile(int id)
{
List<EmployeeModel> ObjFiles = new DataLayer().GetFileList();
var FileById = (from FC in ObjFiles
where [Link](id)
select new { [Link],
[Link] }).ToList().FirstOrDefault();
return File([Link], "application/pdf",
[Link]);
}
}
}

================

[Link]

@model IEnumerable<[Link]>

<table class="table table-bordered">


<tr>
<th class="col-md-4">
@[Link](model => [Link])
</th>

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

@foreach (var item in Model) {


<tr>
<td>
@[Link](modelItem => [Link])
</td>

<td>
@[Link]("Downlaod", "DownLoadFile", new { id=[Link] })

</td>
</tr>
}

</table>
=================

[Link]

@model [Link]

@{
[Link] = "[Link]";
}
<script src="~/Scripts/[Link]"></script>
<script src="~/Scripts/[Link]"></script>
<script src="~/Scripts/[Link]"></script>

@using ([Link]("Upload", "Home", [Link], new {enctype =


"multipart/form-data" }))
{
@[Link]()
<div class="form-horizontal">
<hr />
<div class="form-group">
@[Link](model => [Link])
<div class="col-md-10">
@[Link](model => [Link], new{ @type = "file",
@multiple = "multiple" })
@[Link](model => [Link])
</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">
@[Link]
</div>
</div>

<div class="form-group">
<div class="col-md-8">
@[Link]("Index", "Home")

</div>
</div>
</div>
}

===connection

<connectionStrings>
<add name="Db" connectionString="Data Source=[Link]\omninet;Initial
Catalog=Dhananjay;User Id=sa;Password=sa@123*"
providerName="[Link]"/>
</connectionStrings>

You might also like