Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 640
sandeep.mishra Avatar asked Feb 16 '14 07:02

sandeep.mishra


Video Answer


2 Answers

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>
like image 128
Damith Avatar answered Oct 20 '22 01:10

Damith


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.

like image 27
Simpa Avatar answered Oct 20 '22 01:10

Simpa