Multiple File Upload w/ Compression

This is an extension of some great CSS which had no actual functionality other than a progress bar.

So I tapped into some previous uploaders I  had created and came up with this.

Compression was on the table so I threw that in too.

AssetUploader

     

The C#

  private Boolean keepOriginal = true;

        private Boolean PerformUploadComplete()

        {

  

            UploadDetail Upload = new UploadDetail { IsReady = false };

 

            //if request compression >>> Compress and return result

 

            if (Request[“remove”] != null && System.Convert.ToBoolean(Request[“remove”]))

            {

                keepOriginal = false;

            }

 

            //Let the webservie know that we are not yet ready

            // Upload.IsReady = false;

            if (Request.Files[0] != null && Request.Files[0].ContentLength > 0)

            {

 

                string _creator = String.Empty;

 

                //build the local path where upload all the files

                string path = Server.MapPath(“~/PDF”);

                string fileName = Path.GetFileName(Request.Files[0].FileName);

                

 

                //Build the strucutre and stuff it into DTO

                Upload.Creator = _creator;

                Upload.ContentLength = Request.Files[0].ContentLength;

                Upload.FileName = fileName;

                Upload.UploadedLength = 0;

                //Let the polling process know that we are done initializing …

                Upload.IsReady = true;

 

                //set the buffer size to something larger.

                //the smaller the buffer the longer it will take to download,

                //but the more precise your progress bar will be.

                int bufferSize = 1;

                byte[] buffer = new byte[bufferSize];

                try {

                //Writing the byte to disk

                using (FileStream fs = new FileStream(Path.Combine(path, fileName), FileMode.Create))

                {

                    //As long was we haven’t written everything …

                    while (Upload.UploadedLength < Upload.ContentLength)

                    {

                        //Fill the buffer from the input stream

                        int bytes = Request.Files[0].InputStream.Read(buffer, 0, bufferSize);

                        //Writing the bytes to the file stream

                        fs.Write(buffer, 0, bytes);

                        //Update the number the webservice is polling on to the session

                        Upload.UploadedLength += bytes;

                    }

                }

 

                if(IsCompressed(fileName, path)){ this.litScriptUpdate.Text = “// <![CDATA[
function(){  notify(‘”+ fileName + ” Compressed successfully’); }
// ]]>”; }

                }

                catch (Exception exmsg)

                {

                    return false;

                }

              if (!keepOriginal) { DeleteFile(fileName, path); }

            }         

          return Upload.IsReady;

        }

 The Bloody Html

<html xmlns=”http://www.w3.org/1999/xhtml“>

<head runat=”server”>

    <title></title>

</head>

<body>

    <form id=”form” runat=”server” enctype=”multipart/form-data”>

    </form>

  <zcript>

      

        function notify(message) {

           // alert(message);

           jqXHR.creator.document.getElementById(‘status2’).innerHTML(message);

           jqXHR.creator.document.bgColor = ‘lightgreen’;

        }

    </zript>

     <asp:Literal ID=”litScriptUpdate” runat=”server”></asp:Literal>

</body>

</html>

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s