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

ASP.NET file post direct to disk with upload file progress support

Rate me:
Please Sign up or sign in to vote.
4.77/5 (15 votes)
16 Jul 2006CPOL5 min read 218.3K   5.6K   96  
This module gives developers a way to intercept multi-part form posts in ASP.NET and store the data directly to disk without going direct to memory. It also provides a way to extract file progress from an external window. Written in C# for backend, and JavaScript / Atlas for the progress bar.
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;

public partial class _Default : System.Web.UI.Page
{
    /// <summary>
    /// Method handler for the Page Load event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        // Show the correct panel when the page loads.
        SetPanel();
    }

    /// <summary>
    /// Methed used to setup the correct panel
    /// </summary>
    private void SetPanel()
    {
        // Default the panels to false first.
        pnl_FinishedUpload.Visible = false;
        pnl_upload.Visible = false;

        // Look for certain URL params to determin what panel to make visible
        if (Request.QueryString["finishedfiles"] != null && Request.QueryString["finishedfiles"].Length > 0)
        {
            // Import the images
            //ImportImages();
            pnl_FinishedUpload.Visible = true;

        }
        else if (Request.QueryString["ID"] != null && Request.QueryString["ID"].Length > 0)
        {
            pnl_upload.Visible = true;
        }
        else
        {
            System.Diagnostics.Trace.WriteLine("else");
            pnl_upload.Visible = true;
        }
    }

    #region "The following code has been commented out, but this is an example on how I used the module to provide me with information on uploaded files."

    //private void ImportImages()
    //{
    //    // Get the path where the uploaded files went.
    //    string basePath = global::TravisWhidden.UploadModule.BasePath;

    //    string[] finishedFiles = Request.QueryString["finishedfiles"].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

    //    for (int fileIndex = 0; fileIndex < finishedFiles.Length; fileIndex++)
    //    {
    //        string file = basePath + finishedFiles[fileIndex];

    //        if (File.Exists(file))
    //        {
    //            FileStream fs = null;
    //            try
    //            {
    //                // get the vehicleID first.
    //                Guid vehicleID = new Guid(Request.QueryString["ID"]);

    //                // We do not wnat to attempt to open files that are larger then 5 megs.  We will check the size before we open it.
    //                fs = new FileStream(file, FileMode.Open, FileAccess.Read);
    //                if (fs.Length < 5000000)
    //                {
    //                    byte[] byteData = new byte[fs.Length];
    //                    fs.Read(byteData, 0, byteData.Length);

    //                    // Create the image processor
    //                    ImageProcessor ip = new ImageProcessor(byteData);
    //                    System.Drawing.Point point = ip.GetDimensions();

    //                    bool approved = true;

    //                    if (point.X > 3000)
    //                    {
    //                        lblFinishedFiles.Text += "<br/> Width on one of the images was to wide. Can not exceed 3000 pixels";
    //                        approved = true;
    //                    }

    //                    if (point.Y > 3000)
    //                    {
    //                        lblFinishedFiles.Text += "<br/> Heigth on one of the images was to high. Can not exceed 3000 pixels";
    //                        approved = true;
    //                    }

    //                    if (approved)
    //                    {
    //                        VehiclePicture vp = VehiclePicture.NewVehiclePicture(vehicleID, Guid.NewGuid());
    //                        vp.ImageX = point.X;
    //                        vp.ImageY = point.Y;
    //                        vp.ImageTitle = "Unnamed";
    //                        vp.Sequence = 0;
    //                        vp.Disabled = false;
    //                        vp.ImageDescription = "";
    //                        vp.Save();

    //                        VehiclePictureData vpd = VehiclePictureData.NewVehiclePictureData(vp.PictureID, "ORIGINAL");
    //                        vpd.PictureData = byteData;
    //                        vpd.Save();
    //                    }
    //                }
    //                else
    //                {
    //                    lblFinishedFiles.Text += "<br/> Image can not exceed 5 megs.";
    //                }
    //            }
    //            catch (Exception ex)
    //            {
    //                lblFinishedFiles.Text += "<br/> Error Processing file " + file + " - Error: " + ex.Message;
    //            }
    //            finally
    //            {
    //                // Cleanup
    //                if (fs != null)
    //                {
    //                    fs.Dispose();
    //                    fs.Close();
    //                }
    //                try
    //                {
    //                    // Remove the file now that we are not using it.
    //                    File.Delete(file);
    //                }
    //                catch { }
    //            }
    //        }
    //    }
    //}

    #endregion

    /// <summary>
    /// This method handler is just so the form will post. I do nothing with the data.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions