0

Possible Duplicate:
href image link download on click

i have created a bulleted list as

<asp:BulletedList ID="BulletedList1" runat="server" DisplayMode="HyperLink" 
            DataSourceID="SqlDataSource1" DataTextField="description" 
            DataValueField="link" onclick="BulletedList1_Click">
        </asp:BulletedList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:chaaapaConnectionString6 %>" 
        SelectCommand="SELECT [description], [link] FROM [quiz]">
    </asp:SqlDataSource>

the datavalue field contain the link to the image file . when clicked the image is shown in the browser . Bit i want that a download box is displayed instead.

Community
  • 1
  • 1
Kamran
  • 79
  • 1
  • 8

1 Answers1

1

The image opens in the browser because your code (I think) just generates a hyperlink and the browser recognizes the MIME type and draws it (rather than opening a download prompt). You need to set the response headers to tell the browser to behave differently.

Specifically, you want to set content-disposition.

See: Uses of content-disposition in an HTTP response header

The easiest way to do this is probably to create a simple HTTP handler that writes the image and headers using Response.BinaryWrite(). An empty ASPX page can be a handler, or you can use a generic handler (ASHX file).

This tutorial seems to cover roughly what you are doing/need to do. Make sure to get the content-disposition values/syntax exactly right (browsers can do strange things with malformed headers).

Community
  • 1
  • 1
Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • i have found this code. but i am confused aboutgetting the file name from the link?? string filename=BulletedList1.DataValueField.ToString(); Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename="+filename); Response.ContentType = "image/jpeg"; Response.WriteFile(Server.MapPath(@"~/"+filename)); Response.End(); – Kamran Aug 08 '12 at 06:28
  • You need to pass the filename to the handler as part of the databinding. Or, you could write the file in response to the click event (see sample near the end of this article): http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.bulletedlist.aspx. Alternatively, a Repeater may be easier: http://stackoverflow.com/questions/1097916/customized-bulleted-list-items-in-asp-net – Tim M. Aug 08 '12 at 06:33