How to show the image after upload in asp.net fileupload

sandeep.mishra picture sandeep.mishra · Feb 16, 2014 · Viewed 53.3k times · Source

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..?

Answer

Damith picture Damith · Feb 16, 2014

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>