Wednesday, 13 September 2017

How To Create Web Forms FileUpload In ASP.NET

How To Create Web Forms FileUpload In ASP.NET
 
code , code for FileUpload In ASP.NET , ASP .NET Developer : Asp.net FileUpload, For Example Of FileUpload In Website code , Web developer .
 
Example

Here, we are implementing file upload control in web form.

 WebControls.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebControls.aspx.cs"  
Inherits="WebFormsControlls.WebControls" %> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
        <div> 
            <p>Browse to Upload File</p> 
            <asp:FileUpload ID="FileUpload1" runat="server" /> 
        </div> 
        <p> 
            <asp:Button ID="Button1" runat="server" Text="Upload File" OnClick="Button1_Click" /> 
        </p> 
    </form> 
    <p> 
        <asp:Label runat="server" ID="FileUploadStatus"></asp:Label> 
    </p> 
</body> 
</html> 

Code
 

 WebControls.aspx.cs
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
namespace WebFormsControlls 
{ 
    public partial class WebControls : System.Web.UI.Page 
    { 
        protected System.Web.UI.HtmlControls.HtmlInputFile File1; 
        protected System.Web.UI.HtmlControls.HtmlInputButton Submit1; 
        protected void Page_Load(object sender, EventArgs e) 
        { 
        } 
        protected void Button1_Click(object sender, EventArgs e) 
        { 
            if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0)) 
            { 
                string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName); 
                string SaveLocation = Server.MapPath("upload") + "\\" + fn; 
                try 
                { 
                    FileUpload1.PostedFile.SaveAs(SaveLocation); 
                    FileUploadStatus.Text = "The file has been uploaded."; 
                } 
                catch (Exception ex) 
                { 
                    FileUploadStatus.Text = "Error: " + ex.Message; 
                } 
            } 
            else 
            { 
                FileUploadStatus.Text = "Please select a file to upload."; 
            } 
        } 
    } 
} 

Post a Comment

Whatsapp Button works on Mobile Device only

Start typing and press Enter to search