Get file name : FileUpload « Asp Control « ASP.Net






Get file name


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="FileUpload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>
            Upload File</h1>
        <asp:FileUpload ID="fupAspNetUpload" runat="server" />
        <br />
        <br />
        <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />
        <br />
        <br />
        <input id="fupHtmlUpload" runat="server" type="file" /></div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Configuration;
using System.Collections;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class FileUpload : System.Web.UI.Page
{
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string htmlFilePath = fupHtmlUpload.PostedFile.FileName;

        string aspNetFilePath = fupAspNetUpload.PostedFile.FileName;

        string filePath = fupAspNetUpload.FileName;

        string fileName = Path.Combine(Server.MapPath("."), filePath);
        fupAspNetUpload.SaveAs(fileName);
    }
}

 








Related examples in the same category

1.FileUpload Control
2.Uploads to a special upload folder