Showing posts with label Download. Show all posts
Showing posts with label Download. Show all posts

Wednesday, January 6, 2010

Retrieve Images from fatabase

This is a sample code to retrieve images from data base in a gridview as to upload images to database.

use fileupload control to upload images and on upload button click retrieve image from fileupload control(named as fulimage)by writing

StreamimgStream = fuImage.PostedFile.InputStream;

and insert it into database along with other parameters.

to retrieve image from database in a gridview use generic handler. place a template in gridview and place itemtemplate in it.now add a image here like this.



and in the ImageHandler.ashx get image id from query string and retrive image from database in a datareader then return that image by this code

context.Response.BinaryWrite((Byte[])dr[0]);

here dr[0] means that image is in first field of the database.

Download the complete code here

Thursday, July 23, 2009

Download files in c#

This code shows how to download files of any type from applciation

private void DownloadFile( string fname)
{
try
{
string path = MapPath( fname );
string name = System.IO.Path.GetFileName( path );
string ext = System.IO.Path.GetExtension( path );
string type = "";
// set known types based on file extension
if ( ext != null )
{
switch( ext.ToLower() )
{
case ".htm":
case ".html":
type = "text/HTML";
break;
case ".txt":
type = "text/plain";
break;
case ".doc":
case ".rtf":
type = "Application/msword";
break;
case ".xml":
type = "Application/xml";
break;
case ".zip":
type = "application/zip";
break;
}
}
Response.AppendHeader( "content-disposition","attachment; filename=" + name );
if ( type != "" )
Response.ContentType = type;
Response.WriteFile( path );
}
catch(Exception ex)
{
new PowerXEditor.BL.CommonUtility().RegisterError(ex);
}
Response.End();
}