Question

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

Was it helpful?

Solution

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>

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top