Translate

Wednesday 30 January 2013

Uploading And Downloading Files/Images By Using Linq to sql in MVC3


Uploading the Images/Files And Downloaded the Uploaded Image/Files By Using Linq to sql

Create a Linq to Sql dbml file in model class and Build it...

Linq To Sql:

DBcontext.dbml  :

 FileDump

Set Pk(Id) and Set Auto increment

Id                int  
FileName     Varchar 
FileData       image

Controller Class:

Test.cs

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

namespace MVC3FileUploadDownloadLINQ.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/
       
        public ActionResult FileUpload(HttpPostedFileBase Files)
        {
            DBContextDataContext dataContext = new DBContextDataContext();
            foreach (string upload in Request.Files)
            {
                //create byte array of size equal to file input stream
                byte[] fileData = new byte[Request.Files[upload].InputStream.Length];
                //add file input stream into byte array
                Request.Files[upload].InputStream.Read(fileData, 0, Convert.ToInt32(Request.Files[upload].InputStream.Length));
                //create system.data.linq object using byte array
                System.Data.Linq.Binary binaryFile = new System.Data.Linq.Binary(fileData);
                //initialise object of FileDump LINQ to sql class passing values to be inserted
                FileDump record = new FileDump { FileData = binaryFile, FileName = System.IO.Path.GetFileName(Request.Files[upload].FileName) };
                //call InsertOnsubmit method to pass new object to entity
                dataContext.FileDumps.InsertOnSubmit(record);
                //call submitChanges method to execute implement changes into database
                dataContext.SubmitChanges();
            }
            var returnData = dataContext.FileDumps;
            ViewData.Model = returnData.ToList();
            return View();
        }

        public FileContentResult FileDownload(int id)
        {
            //declare byte array to get file content from database and string to store file name
            byte[] fileData;
            string fileName;
            //create object of LINQ to SQL class
            DBContextDataContext dataContext = new DBContextDataContext();
            //using LINQ expression to get record from database for given id value
            var record = from p in dataContext.FileDumps
                         where p.ID == id
                         select p;
            //only one record will be returned from database as expression uses condtion on primary field
            //so get first record from returned values and retrive file content (binary) and filename
            fileData = (byte[])record.First().FileData.ToArray();
            fileName = record.First().FileName;
            //return file and provide byte file content and file name
            return File(fileData, "text", fileName);
        }

    }
}



Views:

FileUpload.cshtml


@*@model MVC3FileUploadDownloadLINQ.Models.FileDump*@

@{
    ViewBag.Title = "FileUpload";
}

<h2>FileUpload</h2>
<div id="UploadSection">       
    @using (Html.BeginForm("FileUpload", "Test", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <br />
        <p><input type="file" name="fileUpload1" /> </p>
        <p><input type="submit" value="Upload file" /></p>       
    } 
</div>


<div id="ShowList" title="File List">
        <table width="50%">
        <thead>
            <th>
                File ID
            </th>
            <th>
                File Name
            </th>
            <th>
                File Download
            </th>
        </thead>
        <tbody>
       
            @foreach(var rec in ViewData.Model)
            {
       
           <tr>
            <td>
                @rec.ID
            </td>
            <td>
                @rec.FileName
            </td>
            <td>
                @Html.ActionLink("Download", "Filedownload", new { id=rec.ID})        
            </td>
           </tr>
            }
        </tbody>
        </table>
    </div>




Thanks...

Happy Coding

Regards

Ur's Jagan

No comments:

Post a Comment