Chunk assembly - server side ASP.NET C#

Upload core product.
Post Reply
Bogdan
Posts: 12
Joined: Wed Oct 14, 2009 10:56 pm

Chunk assembly - server side ASP.NET C#

Post by Bogdan »

Hi, I've been trying to find code here for putting the file together on the server side after it has been uploaded in chunks. Couldn't find it, so I had to write it myself. Here it is if anyone needs it:

Just call IfChunkAddChuck(String fname) after you've saved the file from Request.Files[] to the disk.

It appends slices with each upload, so it goes like this, imagine we're uploading file "A.txt" in 4 chunks:
"A.txt.1" => "A.txt.1-1"
"A.txt.1-1" + "A.txt.2" => "A.txt.1-2"
"A.txt.1-2" + "A.txt.3" => "A.txt.1-3"
"A.txt.1-3" + "A.txt.4" => "A.txt"

Code: Select all


        public void AppendFileToFile(string BaseFname, string AppendFname)
        {
            FileStream fs_Main = File.Open((BaseFname), FileMode.Append);

            try
            {
                FileStream fs_Chunk = File.Open(AppendFname, FileMode.Open);
                try
                {
                    int bufSize = 1024 * 64;
                    byte[] ab_buff = new byte[bufSize];
                    int i_br = 0;
                    while((i_br = fs_Chunk.Read(ab_buff, 0, ab_buff.Length)) > 0)
                    {
                        fs_Main.Write(ab_buff, 0, i_br);
                    }
                }
                finally
                {
                    fs_Chunk.Close();
                }

            }
            finally
            {
                fs_Main.Close();
            }
        }

        public void IfChunkAddChuck(String fname)
        {

            if (("" + Request.Form["chunkonfly"]) != "onfly")
                return;

            // chunkamount
            if (("" + Request.Form["chunkamount"]).Length == 0)
                return;
            int i_chunkamount = Convert.ToInt32(Request.Form["chunkamount"]);

            if (i_chunkamount <= 1)
                return;

            // chunkid
            if (("" + Request.Form["chunkid"]).Length == 0)
                return;
            int i_chunkid = Convert.ToInt32(Request.Form["chunkid"]);

            if (i_chunkid < 1)
                return;

            if(!fname.EndsWith("." + i_chunkid))
                return;

            String s_FnameBase = fname.Substring(0, fname.Length - ("." + i_chunkid).Length);

            if(i_chunkid == 1)
            {
                File.Move(s_FnameBase + ".1", s_FnameBase + ".1-1");
                return;
            }

            string s_FnameMain = s_FnameBase + ".1-" + (i_chunkid - 1);

            if(!File.Exists(s_FnameMain))
                return;

            AppendFileToFile(s_FnameMain, fname);

            File.Delete(fname);

            if (i_chunkid == i_chunkamount)
            {
                File.Move(s_FnameMain, s_FnameBase);
            }
            else
            {
                File.Move(s_FnameMain, s_FnameBase + ".1-" + i_chunkid);
            }
        }

User avatar
support
Posts: 1503
Joined: Sun Jan 27, 2008 6:19 pm

Re: Chunk assembly - server side ASP.NET C#

Post by support »

You have a C# sample to recompose file from chunks in process.aspx (ASP.NET upload script 2.0) at:
http://www.jfileupload.com/products/tools/index.html

Post Reply