Jump to content

how to upload file in ASP.NET in C#

Recommended Posts

  • 4 years later...

Try this code for file upload

View File

<!DOCTYPE html>  
    <form id="form1" runat="server">  
            <p>Upload File</p>  
            <asp:FileUpload ID="FileUpload" runat="server" />  
            <asp:Button ID="ButtonUpload" runat="server" Text="Upload File" OnClick="Button_Click" />  
        <asp:Label runat="server" ID="FileUploadStatus"></asp:Label>  

aspx.cs file

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
namespace FileUploadControlls  
    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 ((FileUpload.PostedFile != null) && (FileUpload.PostedFile.ContentLength > 0))  
                string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);  
                string SaveLocation = Server.MapPath("upload") + "\\" + fn;  
                    FileUploadStatus.Text = "The file has been uploaded.";  
                catch (Exception ex)  
                    FileUploadStatus.Text = "Error: " + ex.Message;  
                FileUploadStatus.Text = "Please select a file to upload.";  


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...