Image Upload and Download
Table
dbo.FileStore
Id int
FileContent image
FileType Varchar(50)
FileName varchar(50)
Controller:
HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Data.SqlClient;
namespace MVC3DispAndUpImage.Controllers
{
public class HomeController : Controller
{
public bool HasFile(HttpPostedFileBase file)
{
return (file != null
&& file.ContentLength > 0) ? true : false;
}
//upload images in database one by one one if FileUpload
contain file
public ActionResult
Index()
{
foreach (string
upload in Request.Files)
{
if (!HasFile(Request.Files[upload])) continue;
string fileType =
Request.Files[upload].ContentType;
Stream fileStream =
Request.Files[upload].InputStream;
string fileName = Path.GetFileName(Request.Files[upload].FileName);
int fileLength =
Request.Files[upload].ContentLength;
byte[] fileData = new byte[fileLength];
fileStream.Read(fileData, 0, fileLength);
const string
connect = @"Data Source=JAGAN-PC;Initial Catalog=MVC3;Integrated
Security=True";
using (var
conn = new SqlConnection(connect))
{
var qry = "INSERT
INTO FileStore (FileContent, FileType, FileName)VALUES (@FileContent,
@MimeType, @FileName)";
var
cmd = new SqlCommand(qry,
conn);
cmd.Parameters.AddWithValue("@FileContent",
fileData);
cmd.Parameters.AddWithValue("@FileType", fileType);
cmd.Parameters.AddWithValue("@FileName",
fileName);
conn.Open();
cmd.ExecuteNonQuery();
}
}
return View();
}
//store file name in the list from the database
//and store that list object in a ViewBag property
public ActionResult
Download()
{
const string connect
= @"Data Source=JAGAN-PC;Initial
Catalog=MVC3;Integrated Security=True";
List<string>
imgList = new List<string>();
using (var conn = new SqlConnection(connect))
{
var qry = "SELECT
FileContent, FileType, FileName FROM FileStore";
var cmd = new
SqlCommand(qry, conn);
conn.Open();
SqlDataReader rdr =
cmd.ExecuteReader();
while (rdr.Read())
{
imgList.Add(rdr["FileName"].ToString());
}
}
ViewBag.Images = imgList;
return View();
}
//get the file id and return a file to the browser
public FileContentResult
GetFile(int id)
{
SqlDataReader rdr;
byte[] fileContent = null;
string mimeType = "";
string fileName = "";
const string connect
= @"Data Source=JAGAN-PC;Initial
Catalog=MVC3;Integrated Security=True";
using (var conn = new SqlConnection(connect))
{
var qry = "SELECT
FileContent, FileType, FileName FROM FileStore WHERE ID= @ID";
var cmd = new
SqlCommand(qry, conn);
cmd.Parameters.AddWithValue("@ID",
id);
conn.Open();
rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
rdr.Read();
fileContent = (byte[])rdr["FileContent"];
mimeType = rdr["FileType"].ToString();
fileName = rdr["FileName"].ToString();
}
}
return File(fileContent, fileType, fileName);
}
}
}
Views:
Index.cshtml
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<html>
<head runat="server">
<title>Index</title>
</head>
<body>
<div>
@{
using (Html.BeginForm("",
"home", FormMethod.Post,
new { enctype ="multipart/form-data"
}))
{
<input type="file" name="FileUpload1"
/><br />
<input type="file" name="FileUpload2"
/><br />
<input type="submit"
name="Submit"
id="Submit"
value="Upload"
/><br />
}
<h4>
<a href="/Home/Download">Go
for the Download</a></h4>
}
</div>
</body>
</html>
Download.cshtml
@{
ViewBag.Title = "Download";
}
<h2>Download</h2>
<html>
<head runat="server">
<title>Download</title>
</head>
<body>
<div>
<img src= "@Url.Content("~/Content/Images/CMGR.jpg")" alt="CMGR1.jpg" title="CMGR1.jpg" />
@{
int data = 1;
foreach (string img in ViewBag.Images)
{
<h4> @Html.ActionLink(img, "GetFile/"
+ data++)</h4><br />
<img src= "@Url.Content("img")"
alt="ViewBag.Images"
title="ViewBag.Images"
/>
@*foreach(var item in Model)
{
@ if (ViewBag.Images != null)
{
<img
src="@Url.Action("GetFile", "HomeController", new { id
= item.Id })" alt="@item.FileContent" />
<img src="@Model.GetFile(item.Images)"
alt="@item.FileContent"/>
}
}
@Html.DisplayFor(img,"GetFile/",+data++)*@
if (data == 1)
{
<h2>No files are in the database!</h2>
}
}
}
</div>
</body>
</html>
Happy Coding
Regards
Ur's Jagan
No comments:
Post a Comment