Save Image

Discussion in 'ASP .Net' started by David W. Simmonds, Jan 23, 2004.

  1. Is there a way I can have a button on a ASP.NET form that when clicked will
    allow the user to save the image to a file on the client side? I know that
    the user can simply rclick the image and select Save Target as..., but the
    button might be a more intuitive way.
    David W. Simmonds, Jan 23, 2004
    #1
    1. Advertising

  2. You could make the button do a response.redirect("insert path to image
    here")

    Michael

    "David W. Simmonds" <> wrote in message
    news:pzhQb.244902$X%5.43959@pd7tw2no...
    > Is there a way I can have a button on a ASP.NET form that when clicked

    will
    > allow the user to save the image to a file on the client side? I know that
    > the user can simply rclick the image and select Save Target as..., but the
    > button might be a more intuitive way.
    >
    >



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.564 / Virus Database: 356 - Release Date: 1/19/2004
    Michael Pearson, Jan 23, 2004
    #2
    1. Advertising

  3. That just brings up another instance of the image in the window. It does not
    prompt the user to save it. Does anyone know how I might do this or am I
    stuck with the standard method of having the user rclick the image and
    select from the menu?

    "Michael Pearson" <> wrote in message
    news:...
    > You could make the button do a response.redirect("insert path to image
    > here")
    >
    > Michael
    >
    > "David W. Simmonds" <> wrote in message
    > news:pzhQb.244902$X%5.43959@pd7tw2no...
    > > Is there a way I can have a button on a ASP.NET form that when clicked

    > will
    > > allow the user to save the image to a file on the client side? I know

    that
    > > the user can simply rclick the image and select Save Target as..., but

    the
    > > button might be a more intuitive way.
    > >
    > >

    >
    >
    > ---
    > Outgoing mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.564 / Virus Database: 356 - Release Date: 1/19/2004
    >
    >
    David W. Simmonds, Jan 24, 2004
    #3
  4. 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 "ContentType" and Header member. First we set the
    Response.ContentType 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.AddHeader 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.ContentType = "Image/GIF";
    Response.AddHeader( "Content-Disposition",
    "attachment;filename=\"Microsoft.gif\"" );
    Response.WriteFile("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="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.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.Page
    {
    protected System.Web.UI.WebControls.Button btnDownload;
    protected System.Web.UI.WebControls.Label lblMessage;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.btnDownload.Click += new
    System.EventHandler(this.btnDownload_Click);
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void btnDownload_Click(object sender, System.EventArgs e)
    {
    Response.ClearHeaders();
    Response.ClearContent();
    Response.Clear();
    Response.ContentType = "Image/GIF";

    Response.AddHeader("Content-Disposition","attachment;filename=\"MS.gif\"");
    Response.WriteFile(Server.MapPath("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.)
    Steven Cheng[MSFT], Jan 26, 2004
    #4
  5. That works great. Thanks.

    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > 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 "ContentType" and Header member. First we set the
    > Response.ContentType 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.AddHeader 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.ContentType = "Image/GIF";
    > Response.AddHeader( "Content-Disposition",
    > "attachment;filename=\"Microsoft.gif\"" );
    > Response.WriteFile("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="Microsoft Visual Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE" Content="C#">
    > <meta name="vs_defaultClientScript" content="JavaScript">
    > <meta name="vs_targetSchema"
    > content="http://schemas.microsoft.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.Page
    > {
    > protected System.Web.UI.WebControls.Button btnDownload;
    > protected System.Web.UI.WebControls.Label lblMessage;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > // Put user code to initialize the page here
    > }
    >
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArgs e)
    > {
    > //
    > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    > //
    > InitializeComponent();
    > base.OnInit(e);
    > }
    >
    > /// <summary>
    > /// Required method for Designer support - do not modify
    > /// the contents of this method with the code editor.
    > /// </summary>
    > private void InitializeComponent()
    > {
    > this.btnDownload.Click += new
    > System.EventHandler(this.btnDownload_Click);
    > this.Load += new System.EventHandler(this.Page_Load);
    >
    > }
    > #endregion
    >
    > private void btnDownload_Click(object sender, System.EventArgs e)
    > {
    > Response.ClearHeaders();
    > Response.ClearContent();
    > Response.Clear();
    > Response.ContentType = "Image/GIF";
    >
    >

    Response.AddHeader("Content-Disposition","attachment;filename=\"MS.gif\"");
    > Response.WriteFile(Server.MapPath("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.)
    >
    >
    >
    >
    >
    David W. Simmonds, Jan 26, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike John
    Replies:
    0
    Views:
    498
    Mike John
    Jul 28, 2003
  2. user
    Replies:
    1
    Views:
    753
    Bruce Barker
    Apr 4, 2005
  3. teo
    Replies:
    1
    Views:
    453
  4. Daniel Mark
    Replies:
    1
    Views:
    702
    Fredrik Lundh
    Sep 25, 2006
  5. dodgeyb
    Replies:
    1
    Views:
    608
    George Ter-Saakov
    Dec 14, 2007
Loading...

Share This Page