Click here to Skip to main content
15,887,896 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 217.3K   5.5K   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.
<%@ page language="C#" autoeventwireup="true" inherits="UploadProgress, App_Web_uploadprogress.aspx.cdcab7d2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Upload Progress</title>
    <style type="text/css">

	#progressContainer
	{
		border:solid 1px #008000;
		height:20px;
		width:325px;
	}

	#progressBar
	{
		background-color:#00aa00;
		margin:1px;
		height:18px;
	}
	
		
</style>
   
     
</head>
<body>
    <form id="form1" runat="server">
        <table width="350" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    <div>
                        <span style="font-size: 10pt; font-family: Verdana">
                        Status: <strong><span id="CurrentStatus">Please Wait...</span>
                        <br />
                        </strong>
                        Transfered: <span id="TransferedBytes">0</span>
                        <br />
                        Total Bytes: <span id="TotalBytes">0</span>
                        <br />
                        </span>
                        <div id="progressContainer" style="width:325px">
                            <div id="progressBar">
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
        </table>
        
        <span id="trace"></span>

       

        <script language="javascript" type="text/javascript">
    var CurrentStatusObject = document.getElementById("CurrentStatus");
    var TransferedBytes = document.getElementById("TransferedBytes");
    var TotalBytes = document.getElementById("TotalBytes");
    
    var IntervalID = null;
    
    var WaitingForUpdate = false;
    
    // Add the default values to the javascript before we load the information
    addLoadEvent(function() {
        RunStatusUpdates();
    })

    function Trace(message){
        var traceObj = document.getElementById("trace");
        //traceObj.innerHTML = message + "<br />" + traceObj.innerHTML;
    }


    function addLoadEvent(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
                if (oldonload) {
                    oldonload();
                }
                func();
            }
        }
    }
    
    
    function RunStatusUpdates(){
        Trace("starting interval");
        IntervalID = setInterval(GetUpdateStatus, 1000);
    }
    
    function GetUpdateStatus()
    {
        if(!WaitingForUpdate){
            //WaitingForUpdate = true;
            Trace("Getting Status update");
            UploadProcess.GetUploadStatus('<%=Request.QueryString["PostID"]%>', GetUpdateStatusResponse);
        }
        
    }
    
    function GetUpdateStatusResponse(result)
    {
        Trace('response');
        WaitingForUpdate = false;
    
        // Update Status Information:
        try
        {
            if(result == null)
            {
                Trace('null');
                //CurrentStatusObject.innerHTML = "Initializing..."
                
            }
            else
            {
                Trace("Response: TotalSize: " + result.TotalBytes + " - Transfered: " + result.CurrentBytesTransfered);
            
                CurrentStatusObject.innerHTML = "Downloading..."
                TransferedBytes.innerHTML = result.CurrentBytesTransfered;
                TotalBytes.innerHTML = result.TotalBytes;
                Trace("Percentage: " + (result.CurrentBytesTransfered / result.TotalBytes));
                Trace("Previous Width: " + document.getElementById("progressBar").style.width);
                document.getElementById("progressBar").style.width = ((result.CurrentBytesTransfered / result.TotalBytes) * document.getElementById("progressContainer").offsetWidth) -3;
                Trace("New Width: " + document.getElementById("progressBar").style.width);
               
                if(result.CurrentBytesTransfered == result.TotalBytes)
                {
                    if(result.CurrentBytesTransfered != 0)
                    {
                        Trace("Finished.  Close window");
                        // stop the loop. no need to get any more updates.
                        clearInterval(IntervalID);
                        CurrentStatusObject.innerHTML = "Finished";
                        window.close();
                    }
                }
            }
        }
        catch(e){}
    }
    
    
    </script>

    
    </form>
</body>
</html>
<atlas:ScriptManager ID="ScriptManager1" runat="server">
</atlas:ScriptManager>
<atlas:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
<Services>
    <atlas:ServiceReference GenerateProxy="true" Path="UploadProcess.asmx" />
</Services>
</atlas:ScriptManagerProxy>

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