Returns the existing instance of the Upload class, which acts as a wrapper for a file upload process.

Namespace:  EasyAlgo.EAUpload
Assembly:  EasyAlgo.EAUpload (in EasyAlgo.EAUpload.dll)
Version: 1.3.1.0

Syntax

C#
public static Upload GetUpload()
Visual Basic (Declaration)
Public Shared Function GetUpload As Upload

Return Value

The Upload object that is associated with the current request.
If this object is not exist then nullNothingnullptra null reference (Nothing in Visual Basic) will be returned.

Remarks

Returned value is not a new instane and it is a reference to the existing object that had been created by EAUploadModule. EAUploadModule creates new Upload object when another upload request(HTTP POST request with "multipart/form-data" content type) is intercepted.

Instance that is returned by this method provides an access for complete upload only. Use overloaded GetUpload(String) method if you need to get a status information of an upload or to set specific environment.

Examples

The following code example demonstrates how to use the Upload class for access to uploaded files.
CopyC#
// UploadFile.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EasyAlgo.EAUpload;

namespace EAUploadExamples
{
    public class UploadFile : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Button Button1;
        protected System.Web.UI.WebControls.Label lblResponse;

        private void Page_Load(object sender, System.EventArgs e)
        {

        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            // 
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            // 
            InitializeComponent();
            base.OnInit(e);
        }        

        private void InitializeComponent()
        {    
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Button1_Click(object sender, System.EventArgs e)
        {
            lblResponse.Text = "";

            Upload _upload = UploadsManager.GetUpload();

            // Define directory to which an uploaded files will be stored.
            string SaveFolder = Server.MapPath("") + "\\UploadedFiles\\";

            if (_upload != null && _upload.Status.State == UploadState.Complete)
            {
                // Iterate through uploaded files collection.
                for (int i = 0; i < _upload.UploadedFiles.Count; i++)
                {
                    UploadedFile _file = _upload.UploadedFiles[i];

                    // Save an uploaded file to a specific location 
                    // with name that was been on the client's machine.
                    // You can save an uploaded file with any name.
                    // To do that, just specify the required name in "path" parameter of SaveAs method.
                    _file.SaveAs(SaveFolder + _file.QualifiedFileName, true);

                    // Fill the response lable
                    lblResponse.Text += "File uploaded successfully: <br />";
                    lblResponse.Text += "File was saved as: " + _file.StoredFileName + "<br />";
                    lblResponse.Text += "Client file name: " + Server.HtmlEncode(_file.ClientFileName) + "<br />";
                    lblResponse.Text += "Content type: " + _file.ContentType + "<br />";
                    lblResponse.Text += "File size: " + _file.ContentLength + "<br /><br />";                    
                }

                // Retrieve a value of text field from form fields collection 
                // (You can use also Request.Form property to retrieve a value of form field).
                // Use HttpServerUtility.HtmlEncode method when you display data that is inputted by the user.
                // It allows you to prevent cross-site scripting attacks.
                lblResponse.Text += "Comment: " + Server.HtmlEncode(_upload.FormElements["comment"]) + "<br /><br />";    

                if (_upload.UploadedFiles.Count == 0)
                {
                    lblResponse.Text += "You doesn't selected any file.";
                }
            }
            else
            {
                lblResponse.Text += "Upload is unsuccessful! <br />";                    
                if (_upload != null && _upload.LastError != null)
                {
                    lblResponse.Text += "Upload state: " + _upload.Status.State.ToString() + "<br />";
                    lblResponse.Text += "An error was occurred: " + _upload.LastError.Message;
                }
            }        
        }
    }
}

// --------------------------------------------------

// UploadFile.aspx

<%@ Page language="c#" Codebehind="UploadFile.aspx.cs" AutoEventWireup="false" Inherits="EAUploadExamples.UploadFile" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>Upload to the same page.</title>
        <link rel="stylesheet" href="ExamplesStyle.css">
    </HEAD>
    <body>
        This example demonstrates how to upload file to the same page by handling a 
        button click event. Code for files processing is located in codebehind file.
        <br>
        <br>
        <form id="Form1" method="post" runat="server" enctype="multipart/form-data">
            <table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none">
                <tbody>
                    <tr>
                        <td>Select file 1:</td>
                        <td><input type="file" name="file1" size="40" id="file1"></td>
                    </tr>
                    <tr>
                        <td>Select file 2:</td>
                        <td><input type="file" name="file2" size="40" id="file2"></td>
                    </tr>
                    <tr>
                        <td>Comment:</td>
                        <td><input type="text" name="comment"></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>
                            <asp:Button id="Button1" runat="server" Width="80px" Text="Upload"></asp:Button>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br>
            <asp:Label id="lblResponse" runat="server"></asp:Label>
        </form>
    </body>
</HTML>
CopyVB.NET
' UploadFile.aspx.vb


Imports System
Imports System.Web
Imports System.Web.UI

Imports EasyAlgo.EAUpload

Public Class UploadFile
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents lblResponse As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        lblResponse.Text = ""

        Dim _upload As Upload = UploadsManager.GetUpload

        ' Define directory to which an uploaded files will be stored.
        Dim SaveFolder As String = Server.MapPath("") & "\UploadedFiles\"

        If ((Not _upload Is Nothing) AndAlso (_upload.Status.State = UploadState.Complete)) Then
            Dim i As Integer

            ' Iterate through uploaded files collection.
            For i = 0 To _upload.UploadedFiles.Count - 1
                Dim _file As UploadedFile = _upload.UploadedFiles.Item(i)

                ' Save an uploaded file to a specific location 
                ' with name that was been on the client's machine.
                ' You can save an uploaded file with any name.
                ' To do that, just specify the required name in "path" parameter of SaveAs method.                    
                _file.SaveAs(SaveFolder & _file.QualifiedFileName, True)

                ' Fill the response lable
                lblResponse.Text &= "File uploaded successfully: <br />"
                lblResponse.Text &= "File was saved as: " & _file.StoredFileName & "<br />"
                lblResponse.Text &= "Client file name: " & Server.HtmlEncode(_file.ClientFileName) & "<br />"
                lblResponse.Text &= "Content type: " & _file.ContentType & "<br />"
                lblResponse.Text &= "File size: " & _file.ContentLength & "<br /><br />"
            Next i

            ' Retrieve a value of text field from form fields collection 
            ' (You can use also Request.Form property to retrieve a value of form field).
            ' Use HttpServerUtility.HtmlEncode method when you display data that is inputted by the user.
            ' It allows you to prevent cross-site scripting attacks.
            lblResponse.Text &= "Comment: " & Server.HtmlEncode(_upload.FormElements.Item("comment")) & "<br /><br />"

            If (_upload.UploadedFiles.Count = 0) Then
                lblResponse.Text &= "You doesn't selected any file."
            End If

        Else

            lblResponse.Text &= "Upload is unsuccessful! <br />"                
            If ((Not _upload Is Nothing) AndAlso (Not _upload.LastError Is Nothing)) Then
                lblResponse.Text &= "Upload state: " & _upload.Status.State.ToString & "<br />"
                lblResponse.Text &= "An error was occurred: " & _upload.LastError.Message
            End If

        End If

    End Sub

End Class 

' --------------------------------------------------


' UploadFile.aspx


<%@ Page language="vb" Codebehind="UploadFile.aspx.vb" AutoEventWireup="false" Inherits="EAUploadExamples.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>File upload example.</title>
        <link rel="stylesheet" href="ExamplesStyle.css">
    </HEAD>
    <body>
        This example demonstrates how to upload file to the same page by handling a 
        button click event. Code for files processing is located in codebehind file.
        <br>
        <br>
        <form id="Form1" method="post" runat="server" enctype="multipart/form-data">
            <table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none">
                <tbody>
                    <tr>
                        <td>Select file 1:</td>
                        <td><input type="file" name="file1" size="40" id="file1"></td>
                    </tr>
                    <tr>
                        <td>Select file 2:</td>
                        <td><input type="file" name="file2" size="40" id="file2"></td>
                    </tr>
                    <tr>
                        <td>Comment:</td>
                        <td><input type="text" name="comment"></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>
                            <asp:Button id="Button1" runat="server" Width="80px" Text="Upload"></asp:Button>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br>
            <asp:Label id="lblResponse" runat="server"></asp:Label>
        </form>
    </body>
</HTML>

See Also