Upload file to server : FileUpload « ASP.net Controls « ASP.NET Tutorial

Home
ASP.NET Tutorial
1.ASP.Net Instroduction
2.Language Basics
3.ASP.net Controls
4.HTML Controls
5.Page Lifecycle
6.Response
7.Collections
8.Validation
9.Development
10.File Directory
11.Sessions
12.Cookie
13.Cache
14.Custom Controls
15.Profile
16.Configuration
17.LINQ
18.ADO.net Database
19.Data Binding
20.Ajax
21.Authentication Authorization
22.I18N
23.Mobile
24.WebPart
25.XML
ASP.NET Tutorial » ASP.net Controls » FileUpload 




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

<!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>
    <asp:FileUpload ID="Uploader" runat="server" Height="24px" Width="472px" />&nbsp;
        <asp:Button ID="cmdUpload" runat="server" Height="24px" OnClick="cmdUpload_Click"
            Text="Upload" Width="88px" /><br />
        <br />
        <asp:Label ID="lblInfo" runat="server" EnableViewState="False" Font-Bold="True"></asp:Label></div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
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;
using System.IO;

public partial class UploadFile : System.Web.UI.Page 
{
    private string uploadDirectory;
    protected void Page_Load(object sender, EventArgs e)
    {
        uploadDirectory = Path.Combine(Request.PhysicalApplicationPath, "Uploads");
    }
    

    protected void cmdUpload_Click(object sender, EventArgs e)
    {
        if (Uploader.PostedFile.FileName == "")
        {
            lblInfo.Text = "No file specified.";
        }
        else
        {
            string extension = Path.GetExtension(Uploader.PostedFile.FileName);
            switch (extension.ToLower())
            {
                case ".png":
                case ".jpg":
                    break;
                default:
                    lblInfo.Text = "This file type is not allowed.";
                    return;
            }
            string serverFileName = Path.GetFileName(Uploader.PostedFile.FileName);
            string fullUploadPath = Path.Combine(uploadDirectory,serverFileName);

            try
            {
                Uploader.PostedFile.SaveAs(fullUploadPath);

                lblInfo.Text = "File " + serverFileName;
                lblInfo.Text += " uploaded successfully to ";
                lblInfo.Text += fullUploadPath;
            }
            catch (Exception err)
            {
                lblInfo.Text = err.Message;
            }
        }
    }
}














3.19.FileUpload
3.19.1.Accepting File Uploads
3.19.2.FileUpload Test
3.19.3.Upload file to server
3.19.4.Uploading files using the new FileUpload control (C#)
3.19.5.Uploading files using the new FileUpload control (VB)
3.19.6.Changing the file-size limitation setting in the web.config file
3.19.7.The user selects a file from the local disk and the page manages to persist it to a server location
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.