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);
}
}