Hi David,
Thanks for posting in the community! My name is Steven, and I'll be
assisting you on this issue.
From your description, you'd like to let the user manually click a button
to download a certain image file. Just like they use the "save as" menu in
the IE's content menu ,yes?
If there is anything I misunderstood, please feel free to let me know.
As for this problem, I think we can implement this by setting the page's
Response object's "ContentTyp e" and Header member. First we set the
Response.Conten tType as "Image/GIF" or "Image/JPEG" to specify the response
stream as a image. Then, add a Header item into the response using the
Response.AddHea der method to streaming the image as an attachment to the
browser , then in the client the browser will popup a dialog to let the
user choose open or save the certain file. For example:
Response.Conten tType = "Image/GIF";
Response.AddHea der( "Content-Disposition",
"attachment;fil ename=\"Microso ft.gif\"" );
Response.WriteF ile("Microsoft. gif"); // write the file to the response
stream
Also, here is an sample page, you may have a try on it:
-----------------------aspx page-----------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Download </title>
<meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td><FONT face="ËÎÌå">
<asp:Label id="lblMessage " runat="server" Text="Click the button to
download the image!"></asp:Label></FONT></td>
</tr>
<tr>
<td>
<asp:Button id="btnDownload " runat="server"
Text="Download" ></asp:Button></td>
</tr>
</table>
</form>
</body>
</HTML>
-----------------------code behind page class----------------------
public class Download : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Butt on btnDownload;
protected System.Web.UI.W ebControls.Labe l lblMessage;
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.btnDownloa d.Click += new
System.EventHan dler(this.btnDo wnload_Click);
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
private void btnDownload_Cli ck(object sender, System.EventArg s e)
{
Response.ClearH eaders();
Response.ClearC ontent();
Response.Clear( );
Response.Conten tType = "Image/GIF";
Response.AddHea der("Content-Disposition","a ttachment;filen ame=\"MS.gif\"" );
Response.WriteF ile(Server.MapP ath("MS.gif"));
Response.End();
}
}
If you have any further questions, please feel free to post here.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)