Wednesday, March 27, 2013

FileUpload control in asp.net

I have given a example below how to upload file using asp.net FileUpload control.
follow the instruction
1. Drag a file upload control and one button in source code
2. Drag a image control to show the uploaded image
3.Create a folder  name it img
<form id="form1" runat="server">
    <div>
   
        <asp:FileUpload ID="FileUploadControl" runat="server" />
        <asp:Button ID="btnUpload"
            runat="server" Text="Upload" onclick="btnUpload_Click" />
        <br />
        <br />
        <asp:Image ID="Myimage" runat="server" />
    </div>
    </form>
4. copy the code behind below at your button click event

protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUploadControl.HasFile)
        {
            string filename = Path.GetFileName(FileUploadControl.FileName);
            FileUploadControl.SaveAs(Server.MapPath("~/img/")+filename);
            Myimage.ImageUrl = "~/img/" + filename;
        }
    }