ASP.NET Tutorial

.NET Framework

ASP.NET Introduction

ASP.NET Web Forms

Standard Controls

Label Control

TextBox Control

Image Control

Button Controls

Redirecting User

Using HyperLinks

RadioButton Control

RadioButtonList

CheckBox Control

CheckBoxList Control

DropDownList

ListBox Control

ImageMap Control

Event-Driven Programming

ASP.NET Page Structure

IsPostBack

Master Page

View State

Visibility of Controls

Formatting Controls

Applying CSS

Dynamic CSS

Using Style Class

Themes and Skins

Panel Control

PlaceHolder Control

AdRotator Control

Calendar Control

File Upload, Virtual Path

Validation Controls

Page Navigation

User Control

Separating Code-Presentation

Overview of ADO.NET

Data Binding

DataBinding List Control

ASP.Net Virtual Path

In an ASP.NET application the framework controls recognize a tilde ('~') as a shortcut to the root of the application's virtual directory. So the framework will expand a tilde to the value of HttpRuntime.AppDomainAppVirtualPath.

Example 1: Virtual Path

Response.Redirect(@"~/Default.aspx"); //will redirect to the page "Default.aspx" in the root of the application

Response.Redirect(@"~/User/Login.aspx"); //will redirect to the page "Login.aspx" in the folder "User" off the root.

Response.Redirect(@"~/Admin/Login.aspx");

<asp:Image ID="image1" runat="server" ImageUrl="~/images/logo.gif"/>

Response.Write(HttpRuntime.AppDomainAppVirtualPat
h);

Server.MapPath specifies the relative or virtual path to map to a physical directory.

Server.MapPath(".") - returns the current physical directory of the file (e.g. aspx) being executed

Server.MapPath("..") returns the parent directory

Server.MapPath("~") returns the physical path to the root of the application

Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)

Example 2: if you call Server.MapPath() in following request: http://www.example.com/shop/products/GetProduct.aspx?id=2342 then,

Server.MapPath(".")1 returns D:\WebApps\shop\products
Server.MapPath("..") returns D:\WebApps\shop
Server.MapPath("~") returns D:\WebApps\shop
Server.MapPath("/") returns C:\Inetpub\wwwroot
Server.MapPath("/shop") returns D:\WebApps\shop

ASP.NET FileUpload

ASP.NET FileUpload control allow you to browse for and select the file to be uploaded. Once, the user has select the file, the SaveAs method of the FileUpload control can be called to save the file to the disk.

The basic syntax of FileUpload is:

asp.net file code:
<asp:FileUpload ID="FileUpload1" runat="server" />

Code behind:
protected void Button1_Click(object sender, EventArgs e)
{
    FileUpload1.SaveAs("c:\\file.jpg");
}
The FileUpload properties
Properties Description
FileName Returns the name of the file to be uploaded.
HasFile Specifies whether the control has a file to upload.
AllowMultiple It is used to allow upload multiple files.
The HttpPostedFile class has the following properties:
Properties Description
ContentLength Returns the size of the uploaded file in bytes.
ContentType Returns the MIME type of the uploaded file.

Example 3: FileUpload

protected void Button1_Click(object sender, EventArgs e)  
{  
    if (FileUpload1.HasFile)
    {
        try  
        {  
            FileUpload1.SaveAs("c:\\" + FileUpload1.FileName);
            Label1.Text = "File Uploaded Sucessfully !! ";
        }  
        catch (Exception ex)  
        {  
            Label1.Text = "File Not Uploaded!!" + ex.Message.ToString();  
        } 
    }		
    else  
    {  
        Label1.Text = "Please Select File and Upload Again";  
    }  
}

You can use date, time for dynamic file name generate. You can also use Server.MapPath for virtual path.

Example 4: FileUpload with datetime and Server.MapPath

protected void Button1_Click(object sender, EventArgs e)  
{
    String filename = DateTime.Now.ToString("yyyyMMddHHmmss") + System.IO.Path.GetExtension(FileUpload1.FileName);
    if (FileUpload1.HasFile)
    {
        try  
        {  
            FileUpload1.SaveAs(Server.MapPath("~\\") + filename);
            Label1.Text = "File Uploaded Successfully !! ";
        }  
        catch (Exception ex)  
        {  
            Label1.Text = "File Not Uploaded!!" + ex.Message.ToString();  
        } 
    }		
    else  
    {  
        Label1.Text = "Please Select File and Upload Again";  
    }  
}

By default, in ASP.Net the maximum size of a file to be uploaded to the server is around 4MB. However you can increase file size.
<httpRuntime maxRequestLength="10240" /> //10mb
Write the above code in Web.config file.

Example 5: FileUpload Multiple file

protected void Button1_Click(object sender, EventArgs e)  
{  
    if (FileUpload1.HasFile)
    {  
        var count = 0;
        foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)  
        {  
            string fn = System.IO.Path.GetFileName(uploadedFile.FileName);  
            string SaveLocation = Server.MapPath("upload") + "\\" + fn;  
            try  
            {  
                uploadedFile.SaveAs(SaveLocation);  
                count++;  
            }  
            catch (Exception ex)  
            {  
                Label1.Text = "Error: " + ex.Message;  
            }  
        }  
        if (count > 0)  
        {  
            Label1.Text = count + " files has been uploaded.";  
        }  
    }  
    else  
    {  
        Label1.Text = "Please select a file to upload.";  
    }  
}