Join Dotnetcodes DotnetCodes.com is online Discussion Forum for Software professionals . It lets you find friends around the world and Create professional network that share similar interests as you. Get help on ur projects by industry specialists. Also get answers to all ur technical/placement related querries.Get an edge over others.
Already MemberClick here to login
ASP.net MVC Interview Questions Answers Interview Questions
Get Started Developing for Android Apps with Eclipse Articles
Serial Number in SSRS Articles
.Net framework 4.0 Interview Questions Answers Interview Questions
SQL server reporting services Interview Questions (SSRS) part 1 Articles
Whats New in ASP.NET 4.0 Part 2 Articles
How to Print a Crystal Report direct to printer Articles
Difference between Encapsulation and Abstraction Interview Questions
Explaining SDLC -System Development Life Cycle Articles
Html5 interview questions and answers Interview Questions
SharePoint 2010 interview Questions Answers Interview Questions
Infosys Interview Questions Interview Questions
Dynamic Menu using HTML List Tag and CSS in ASP.Net Articles
Populate or bind Dropdownlist in Asp.net using Jquery and Json Articles
ASP.NET MVC Overview Interview Questions
Submit Articles | More Articles..

How to use ashx page to image upload

Posted By: Jaishree On:10/3/2010 11:35:10 AM in:Source Codes Category:ASP.NET Hits:2675
In This Article you learn how to use ashx page for image uploading.

Step 1: Create a ashx Page

Step 2: copy this code into your ashx page.


  1. using System;

  2. using System.Drawing;

  3. using System.IO;

  4. using System.Collections;

  5. using System.Web;

  6. using System.Web.UI;

  7. using System.Web.UI.WebControls;

  8. using System.Web.Security;

  9. using System.Configuration;

  10. using System.Data.SqlClient;

  11. using System.Data;


  12. public class AdvertisementHandler : IHttpHandler 

  13. {

  14.     SqlConnection sqlCon=new SqlConnection(ConfigurationSettings.AppSettings["connectionstring"]);

  15.     SqlCommand sqlCmd;

  16.     SqlDataReader rdr = null;

  17.     public void ProcessRequest (HttpContext context) {


  18.         try

  19.         {

  20.             sqlCmd = new SqlCommand("Usp_Get_AdvertisementByid",sqlCon);

  21.             sqlCmd.CommandType = CommandType.StoredProcedure;

  22.             sqlCon.Open();

  23.             sqlCmd.Parameters.Add(new SqlParameter("@AdvertisementId", context.Request.QueryString["AdverTisementId"].ToString()));

  24.             rdr = sqlCmd.ExecuteReader();

  25.             while (rdr.Read())

  26.             {

  27.                 context.Response.ContentType = "image/jpeg";

  28.                 context.Response.BinaryWrite((byte[])rdr["AdvertisementFile"]);

  29.             }

  30.             if (rdr != null)

  31.                 rdr.Close();

  32.             /// if developer has to load static image then use this

  33.             ///  ReadImage(WriteImage(context.Server.MapPath("~/image/banner.jpg")), context);

  34.         }

  35.         catch (Exception ex)

  36.         {

  37.             ReadImage(WriteImage(context.Server.MapPath("~/image/ad1.jpg")), context);

  38.         }

  39.         finally

  40.         {

  41.             sqlCon.Close();

  42.         }

  43.     }


  44.     private byte[] WriteImage(string filename)//convert image file to byte[]

  45.     {

  46.         FileStream fs = File.OpenRead(filename);

  47.         byte[] buffer = new byte[fs.Length];

  48.         fs.Read(buffer, 0, buffer.Length);

  49.         fs.Close();

  50.         return buffer;

  51.     }

  52.     private void ReadImage(byte[] img,HttpContext context)//show the byte[] image

  53.     {

  54.         // Set up the response settings

  55.         context.Response.ContentType = "image/jpg";

  56.         context.Response.Cache.SetCacheability(HttpCacheability.Public);

  57.         context.Response.BufferOutput = false;


  58.         Stream stream = new MemoryStream(img);

  59.         const int buffersize = 1024 * 16;

  60.         byte[] buffer = new byte[buffersize];

  61.         int count = stream.Read(buffer, 0, buffersize);

  62.         while (count > 0)

  63.         {

  64.             context.Response.OutputStream.Write(buffer, 0, count);

  65.             count = stream.Read(buffer, 0, buffersize);

  66.         }

  67.     }

  68.     public bool IsReusable

  69.     {

  70.         get

  71.         {

  72.             return false;

  73.         }

  74.     }

  75. }

Then  Set Image Url of control Image

Img.ImageUrl = 

"Handler.ashx?AdverTisementId="+dsFile.Tables[0].Rows[i]["AdvertiseMentId"].ToString() + "";

comments powered by Disqus
User Profile
Jaishree
Sr. Engineer
Delhi , India
Email :You must Log In to access the contact details.
Latest Post from :Jaishree
Function to Convert Text String to Title Case – Proper Case
View: 903 | Submitted on: 3/6/2013 11:23:44 PM
Uploading an image directly into the database and retreive in the PictureBox control in Windows Forms Application
View: 1870 | Submitted on: 9/21/2012 3:05:29 AM
Oracle Interview Questions part 2
View: 1252 | Submitted on: 1/7/2011 3:45:40 AM
Oracle Interview Questions part 1
View: 1094 | Submitted on: 1/7/2011 3:43:40 AM
Oracle Interview Questions
View: 1190 | Submitted on: 1/7/2011 3:36:49 AM
Submit Articles | All Post of This User..

All rights reserved to dotnetcodes. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
Best viewed at 1024 x 768 resolution with Internet Explorer 5.0 or Mozila Firefox 3.5 or Google Crome and higher