Makhado V Makhado V - 5 months ago
235 0

How to Save and Retreive Images from database using LinQ to SQL

C#

Save & Retrieve from DB

Default.aspx.cs

    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Web;
    5 using System.Web.UI;
    6 using System.Web.UI.WebControls;
    7 using System.Data.Linq;
    8 using System.IO;
    9 
   10 namespace uploadingImageusingLInqtoSQl
   11 {
   12     public partial class _Default : System.Web.UI.Page
   13     {
   14         protected void Page_Load(object sender, EventArgs e)
   15         {
   16 
   17         }
   18 
   19         protected void Button1_Click(object sender, EventArgs e)
   20         {
   21             if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 0)
   22             {
   23                 string fileName = FileUpload1.FileName;           
   24 
   25                 byte[] fileByte = FileUpload1.FileBytes;
   26                 Binary binaryObj = new Binary(fileByte);
   27                 DataClasses1DataContext context = new DataClasses1DataContext();
   28                 context.ImageTables.InsertOnSubmit(
   29                     new ImageTable { Id = “xyz”,
   30                         Name = fileName,
   31                         FileSource = binaryObj });
   32                 context.SubmitChanges();
   33 
   34 
   35             }
   36         }
   37 
   38         protected void Button2_Click(object sender, EventArgs e)
   39         {
   40 
   41 
   42             Image1.ImageUrl = “~/MyPhoto.ashx?Id=”+TextBox1.Text;
   43 
   44         }
   45 
   46 
   47     }
   48 }
   49 
 
MyPhoto.ashx.cs
 
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Web;
    5 using System.IO;
    6 using System.Web.UI;
    7 using System.Web.UI.WebControls;
    8 
    9 
   10 namespace uploadingImageusingLInqtoSQl
   11 {
   12     /// <summary>
   13     /// Summary description for MyPhoto
   14     /// </summary>
   15     public class MyPhoto : IHttpHandler
   16     {
   17 
   18         public void ProcessRequest(HttpContext context)
   19         {
   20 
   21             string id = context.Request.QueryString[“Id”];
   22             context.Response.ContentType = “image/jpeg”;
   23             Stream strm = ShowEmpImage(id);
   24             byte[] buffer = new byte[4096];
   25             int byteSeq = strm.Read(buffer, 0, 4096);
   26             while (byteSeq > 0)
   27             {
   28                 context.Response.OutputStream.Write(buffer, 0, byteSeq);
   29                 byteSeq = strm.Read(buffer, 0, 4096);
   30             }  
   31         }
   32 
   33         public Stream ShowEmpImage(string id)
   34         {
   35             DataClasses1DataContext context1 = new DataClasses1DataContext();
   36             var r = (from a in context1.ImageTables where a.Id == id select a).First();
   37             return new MemoryStream(r.FileSource.ToArray());         
   38 
   39         }
   40         public bool IsReusable
   41         {
   42             get
   43             {
   44                 return false;
   45             }
   46         }
   47     }
   48 }
Comments