0

I am creating an application that will show an image when I click a button and browse on the dialog.

this is my sample layout and code:

+---------------------------------------------------+
|                                                   |
|                                                   |
|                                                   |
|          CLICK HERE TO INSERT IMAGE               |
|                                                   |
|                                                   |
|                                                   |
+---------------------------------------------------+

<asp:Panel ID="Panel2" runat="server" cssClass="containment-wrapper" style="border:1px solid #000000;">
            <asp:FileUpload ID="FileUpload1" runat="server" style="display:none;"/>
            <asp:ImageButton ID="MainImage" runat="server" CssClass="mainImage" ImageUrl="~/Image/ClickHere.png" OnClick="MainImage_Click"/>
</asp:Panel>

and my .CS file

  protected void Page_Load(object sender, EventArgs e)
    {
        MainImage.Attributes.Add("onclick", "document.getElementById('" + FileUpload1.ClientID + "').click();");

    }
   protected void MainImage_Click(object sender, ImageClickEventArgs e)
    {

    }

I can click the "CLICK HERE TO INSERT IMAGE" and shows the file dialog, but my problem is how to get the selected image and show it on the buttonImage.

2
  • Do you mean - show the selected image on the ImageButton before it's uploaded to the server ? Commented Jan 13, 2014 at 5:14
  • Not really. Just after I click the "CLICK HERE TO INSERT IMAGE" browse the image, and show it on the ImageButton. Commented Jan 13, 2014 at 5:21

1 Answer 1

1

Then You need to use the FileUpload's PostedFile.SaveAs function to upload the image to a named folder of your website, and set the ImageURL property of the ImageButton as appropriate: -

FileUpload1.PostedFile.SaveAs(HttpContext.Current.Server.MapPath("~/upload/image.jpg"));

Make sure the user your app pool is running under has write permissions to this folder (upload in the example above)

Edit: -

If you don't want to save the image, your task is harder but could be solved by: -

  1. Storing the PostedFile's InputStream to a Session variable
  2. Create an HttpHandler that returns this stream
  3. Point your ImageButton's ImageURL at the HttpHandler instead of an image URL
Sign up to request clarification or add additional context in comments.

1 Comment

What if I don't want to save on my folder just for preview purposes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.