Wylan Osorio
Wylan Osorio

Reputation: 1166

asp.net automatically download image on click on the image

i am new to asp.net and would like to know or a sample of code on how to automatically download image on click on the image. My images are dynamically generated. This is the code where my images generated from code behind.

foreach (HtmlNode n in nodes)
    {
        string img = n.InnerHtml;
        if (n.InnerHtml.Contains(" ")) { img = n.InnerHtml.Replace(" ", "%20"); }
        count++; 
        if (count == 1) {
            newhtml = "<img src =http://img.crwd.io/" + img+" width=\"320px\">";
            table.Rows.Add(new TableRow());
            table.Rows[row].Cells.Add(new TableCell());
            table.Rows[row].Cells.Add(new TableCell());
            table.Rows[row].Cells.Add(new TableCell());
            table.Rows[row].Cells.Add(new TableCell());
            table.Rows[row].Cells[0].Text = newhtml;
        }
        else if (count == 4) {
            newhtml = "<img src =http://img.crwd.io/" + img + " width=\"320px\">";
            table.Rows[row].Cells[3].Text = newhtml; 
            count = 0; 
            row++; }
        else if (count == 3) { 
            newhtml = "<img src =http://img.crwd.io/" + img + " width=\"320px\">"; 
            table.Rows[row].Cells[2].Text = newhtml; }
        else if (count == 2) { 
            newhtml = "<img src =http://img.crwd.io/" + img + " width=\"320px\">"; 
            table.Rows[row].Cells[1].Text = newhtml; }
    }

and i wanted when image is clicked it will automatically downloaded from user if possible to a specific location. Thanks.

Upvotes: 1

Views: 1201

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

Just make them a link:

var url = string.Format("http://img.crwd.io/{0}", img);
newhtml = string.Format("<a href=\"{0}\" target=\"_blank\">" +
    "<img src=\"{0}\" width=\"320px\"></a>", url);

If you do it this way, the browser will handle everything for you.


UPDATE: based on the comments below, an ActiveX control needs to be created.

Upvotes: 2

Related Questions