private void SaveAs( string saveFilePath, HttpPostedFile file)
{
long lStartPos = 0;
int startPosition = 0;
int endPosition = 0;
var contentRange=HttpContext.Current.Request.Headers[ "Content-Range" ];
//bytes 10000-19999/1157632
if (! string .IsNullOrEmpty(contentRange))
{
contentRange = contentRange.Replace( "bytes" , "" ).Trim();
contentRange = contentRange.Substring(0, contentRange.IndexOf( "/" ));
string [] ranges = contentRange.Split( ‘-‘ );
startPosition = int .Parse(ranges[0]);
endPosition = int .Parse(ranges[1]);
}
System.IO.FileStream fs;
if (System.IO.File.Exists(saveFilePath))
{
fs = System.IO.File.OpenWrite(saveFilePath);
lStartPos = fs.Length;
}
else
{
fs = new System.IO.FileStream(saveFilePath, System.IO.FileMode.Create);
lStartPos = 0;
}
if (lStartPos > endPosition)
{
fs.Close();
return ;
}
else if (lStartPos < startPosition)
{
lStartPos = startPosition;
}
else if (lStartPos > startPosition && lStartPos < endPosition)
{
lStartPos = startPosition;
}
fs.Seek(lStartPos, System.IO.SeekOrigin.Current);
byte [] nbytes = new byte [512];
int nReadSize = 0;
nReadSize = file.InputStream.Read(nbytes, 0, 512);
while (nReadSize > 0)
{
fs.Write(nbytes, 0, nReadSize);
nReadSize = file.InputStream.Read(nbytes, 0, 512);
}
fs.Close();
}
|