Click here to Skip to main content
15,892,005 members
Articles / Web Development / ASP.NET

Saving temporary files in web applications

Rate me:
Please Sign up or sign in to vote.
3.59/5 (5 votes)
18 Sep 2007CPOL4 min read 65.1K   902   23  
As you know, the main difference between web and desktop applications is one is stateless and the other is not. What if you need to save files in a temporary place before inserting them into a database? This article describes the problem and has a solution.
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
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 _Default : System.Web.UI.Page 
{
    private List<DocumentFile> _documents
    {
        get
        {
            if (ViewState["Documents"] == null)
                ViewState["Documents"] = new List<DocumentFile>();
            return (List<DocumentFile>)ViewState["Documents"];
        }
        set
        {
            ViewState["Documents"] = value;
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            DataBind();
    }

    public override void DataBind()
    {
        base.DataBind();
        grdDocuments.DataSource = _documents;
        grdDocuments.DataBind();
    }

    protected void lnkAddDocument_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            TempUpload tempUpload = new TempUpload(fileDocument);
            String documentPath = tempUpload.SaveFile();
            _documents.Add(new DocumentFile(Guid.NewGuid().ToString(), txtDocumentTitle.Text, documentPath));
            DataBind();
            txtDocumentTitle.Text = String.Empty;
        }
    }

    protected void butAddFolder_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            // 1. Insert Folder in table and get ID

            // 2. Using Folder ID insert All Uploaded Documents
            //    and move files from Temp folder to permanent storage

            foreach (DocumentFile document in this._documents)
            {

            }
        }
    }

    protected void grdDocuments_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "DeleteDocument":
                foreach (DocumentFile document in _documents)
                {
                    if (document.ID.Equals(e.CommandArgument))
                    {
                        _documents.Remove(document);
                        break;
                    }
                }
                DataBind();
                break;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Ukraine Ukraine
Alexander is freelance web developer with 4 years experience. He has skills in ASP.NET/MS SQL Server, PHP/MySQL, Ruby On Rails, XHTML, CSS. You can contact with me by seigo.ua@gmail.com. Also check my homepage at www.klalex.com.

Comments and Discussions