Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I am trying to upload an image.I haev added an upload control to my web page as
CSS
Upload front Image<input id="File1"
        style="Z-INDEX: 100; LEFT: 300px; WIDTH: 288px; POSITION: absolute; TOP: 120px; HEIGHT: 22px"
        type="file" size="28" name="Upload" runat="server" >

XML
<div>
        <asp:Button ID="btnPreview" runat="server" Text="Preview Images"
            onclick="btnPreview_Click" />
    </div>

XML
<asp:Panel id="Panel1" runat="server" Height="1000px" Width="800px" Visible = "true">
    <asp:Image ID="Image6" runat="server" />
    </asp:Panel>


When the user clicks on a preview button i want to display the selected image in an image control.
i am getting teh path of the uploaded file in File1.Value and assigning it as teh ImageUrl but no image is being displayed.
Teh images in have selected are .bmps.
Any help.. please?

C#
protected void btnPreview_Click(object sender, EventArgs e)
      {
          if (File1.PostedFile != null)
          {

C#
Image6.ImageUrl = File1.Value;

}


}
Posted

As you have to save the uploaded file in your server at one location, and give the url of that location to the HtmlImage object
shown below code may help you to understand
C#
string path = Server.MapPath("") + @"\UserImages\image" + CurrentImage + ".jpg";
            //This will create one path for CurrentImage.jpg
            FileStream fs = File.Create(path);
            fs.Write(ImageUpload.FileBytes, 0, ImageUpload.FileBytes.Length);
            fs.Close();
           //saving the uploaded file in that path
            Image6.ImageUrl=path;// or \UserImages\image" + CurrentImage + ".jpg";
 
Share this answer
 
Create folder in ur project and save the images on it and retrivew from it
 
Share this answer
 
this works fine for me

you can do it using javascript am posting my example and will explain you all

//this is the script write in head part of the page

XML
<script type="text/javascript">
   function showimg()
   {
   var path= document.getElementById('<%=txt1.ClientID %>').value;
   document.getElementById('<%=img1.ClientID %>').src=path;
   }
   </script>




//your design should be like this i mean body of the page

XML
<body>
    <form id="form1" runat="server">
    <div>
        <table width="100%">
            <tr>
                <td>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td align="left" width="40%">
                    <asp:FileUpload ID="file1" runat="server" />
                </td>
                <td align="left">
                    <asp:Button ID="btnview" runat="server" Text="Preview" OnClick="btnview_Click"  />
                </td>
            </tr>
            <tr>
                <td align="left">
                    <asp:TextBox ID="txt1" runat="server" />
                </td>
                <td align="left">
                    <asp:Image ID="img1" runat="server" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>





//write this in your code behind

XML
protected void btnview_Click(object sender, EventArgs e)
    {
        HttpPostedFile hp = file1.PostedFile;
        txt1.Text = hp.FileName;
        img1.ImageUrl = hp.FileName.ToString();
        Page.ClientScript.RegisterStartupScript(this.GetType(), "aa", "<script language='javascript'>showimg();</script>");
    }
 
Share this answer
 
Comments
AnnSJ 8-Nov-11 7:24am    
hey.. thanx..i haev implemented it,. but still my image does get displayed properly.
when you set the image .src it should be like "~/image.jpg"
but when you copy teh path from teh text box teh forward slashes are used.
Could that be giving me problem?
hitech_s 9-Nov-11 1:55am    
ok
can you please accept my solution if it really useful

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900