8

I have a fileupload control which is inside update panel. I want to display the image after upload is complete. below is my html code

<form id="form1" runat="server">
    <br />
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <br />
    <div>
        <br />
        <table width="50%" cellpadding="2" cellspacing="0">
            <br />
            <tr>
                <br />
                <td>
                    <br />
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
                        <ContentTemplate>
                            <br />
                            <asp:FileUpload ID="FileUpload1" runat="server" /><br />
                            <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /><br />
                        </ContentTemplate>
                        <Triggers> <asp:PostBackTrigger ControlID="btnUpload" /> </Triggers>  
                    </asp:UpdatePanel>
                    <br />
                      <asp:Image ID="imgViewFile" runat="server" />
                </td>
            </tr>  
        </table>
        <br />
    </div>
    <br />
</form>

Below is mycode

protected void btnUpload_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        FileUpload1.SaveAs(MapPath("~/TEST/" + FileUpload1.FileName));
        imgViewFile.ImageUrl = Server.MapPath("~/TEST/" + FileUpload1.FileName);
    }       
}

But the image is not showing the file after upload. Can anybody help me on this..?

Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
sandeep.mishra
  • 825
  • 4
  • 19
  • 40

2 Answers2

6

set path as

imgViewFile.ImageUrl = "~/TEST/" + FileUpload1.FileName;

and aslo put your image inside update panel

     <br />
    <asp:Image ID="imgViewFile" runat="server" />
</asp:UpdatePanel>
Damith
  • 62,401
  • 13
  • 102
  • 153
1

I had this problem also, & I did what Damith suggested, yet it didn't work until, in my own case, I noticed I was using AsyncnPostBackTrigger in my Trigger, instead of PostBackTrigger. Then, it started to work. You may want to check if you make same mistake.

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
Simpa
  • 151
  • 6