Click here to Skip to main content
16,010,553 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have one database containing image table. It is working fine. It uploads images using file upload control and retrieves images using DataList control.

My main problem is if anyone of you have noticed that when we right click on the image coming from the database to save it in our PC, the option "save image. . ." is not coming in the list. I want my website's user to save these images in his/her PC by right click on the image.

Can anyone suggest how to make the user able to save images into his/her PC by right clicking on the image?
Or is there any other way to save/download images from database back to our PC?
Posted
Updated 26-Aug-11 18:19pm
v2

1 solution

I use an ASPX file:
C#
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="wm5ftdl.aspx.cs" Inherits="wm5ftdl" %>

<%
    // Send a download file to the client given the filename.
    string guid = Request.QueryString["file"];
    string fileName = "ERROR";
    byte[] data = new byte[] { 0, 0, 0, 0 };
    string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DownloadDatabase"].ConnectionString;
    using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon))
        {
        con.Open();
        string strcmd = "SELECT [iD] ,cn.[fileName],[description] ,[dataContent] ,[version] " +
                        "FROM dlContent cn " +

                        "WHERE cn.iD=@ID";
        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(strcmd, con))
            {
            cmd.Parameters.AddWithValue("@ID", guid);
            using (System.Data.SqlClient.SqlDataReader r = cmd.ExecuteReader())
                {
                if (r.Read())
                    {
                    fileName = (string) r["filename"];
                    data = (byte[]) r["dataContent"];
                    }
                }
            }
        }
    Response.Clear();
    Response.AddHeader("Cache-Control", "no-cache, must-revalidate, post-check=0, pre-check=0");
    Response.AddHeader("Pragma", "no-cache");
    Response.AddHeader("Content-Description", "File Download");
    Response.AddHeader("Content-Type", "application/force-download");
    Response.AddHeader("Content-Transfer-Encoding", "binary\n");
    Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
    Response.BinaryWrite(data);
    Response.End();
%>


I can then set links to it as
href=\"wm5ftdl.aspx?file=IdOfImage"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900