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

54,494

Solution 1

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>

Solution 2

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.

Share:
54,494
sandeep.mishra
Author by

sandeep.mishra

Updated on September 09, 2020

Comments

  • sandeep.mishra
    sandeep.mishra over 3 years

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