<input type=file/>
, less often - with the help of Flash, even less often - by other means (we do not consider downloading via FTP in this article).
SessionId = (1100000000 + new Random ().Next(10000000, 99999999)).ToString();
* This source code was highlighted with Source Code Highlighter .
UniqueKey = "" ;
try
{
if (FileLength < Constants.MinFilesizeToAdd)
{
throw new Exception();
}
// Adler32 version to compute "unique" file hash
// UniqueKey will be Constants.NumPoints * sizeof(uint) length
int part_size = ( int )((file.Length / Constants.NumPoints) < Constants.MaxPartSize ? file.Length / Constants.NumPoints : Constants.MaxPartSize);
byte [] buffer = new Byte [part_size];
byte [] adler_sum = new Byte [Constants.NumPoints * sizeof ( uint ) / sizeof ( byte )];
int current_point = 0;
int bytesRead = 0;
Stream fs = file.OpenRead();
AdlerChecksum a32 = new AdlerChecksum();
while (current_point < Constants.NumPoints && (bytesRead = fs.Read(buffer, 0, part_size)) != 0)
{
a32.MakeForBuff(buffer, bytesRead);
int mask = 0xFF;
for ( int i = 0; i < sizeof ( uint ) / sizeof ( byte ); i++)
{
UniqueKey += ( char )((mask << (i * sizeof ( byte )) & a32.ChecksumValue) >> (i * sizeof ( byte )));
}
fs.Position = ++current_point * file.Length / Constants.NumPoints;
}
}
catch (Exception) { }
* This source code was highlighted with Source Code Highlighter .
UriBuilder ub = new UriBuilder(UploadUrl);
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(ub. Uri );
webrequest.Method = "POST" ;
webrequest.ContentType = "application/octet-stream" ;
// Some russian letters in filename lead to exception, so we do uri encode on client side
// and uri decode on server side
webrequest.Headers[ "Content-Disposition" ] = "attachment; filename=\"" + HttpUtility.UrlEncode( File .Name) + "\"" ;
webrequest.Headers[ "X-Content-Range" ] = "bytes " + currentChunkStartPos + "-" + currentChunkEndPos + "/" + FileLength;
webrequest.Headers[ "Session-ID" ] = SessionId;
webrequest.BeginGetRequestStream( new AsyncCallback(WriteCallback), webrequest);
* This source code was highlighted with Source Code Highlighter .
if (ResponseText != null && ResponseText.Length != 0)
{
// We cannot check response.StatusCode, see comments in constructor of FileUploadControl
if (Regex.IsMatch(ResponseText, @"^\d+-\d+/\d+" )) // we got 201 response
{
...
}
else // we got 200 response
{
BytesUploaded = FileLength;
}
}
* This source code was highlighted with Source Code Highlighter .
webrequest.Headers[ "Content-Disposition" ] = "attachment; filename=\"" + File .Name + "\"" ;
* This source code was highlighted with Source Code Highlighter .
webrequest.Headers[ "Content-Disposition" ] = "attachment; filename=\"" + HttpUtility.UrlEncode( File .Name) + "\"" ;
* This source code was highlighted with Source Code Highlighter .
public long FileLength
{
get { return fileLength; }
set
{
fileLength = value ;
ChunkSize = ( long )(fileLength / (100 / Constants.PercentPrecision));
if (ChunkSize < Constants.MinChunkSize)
ChunkSize = Constants.MinChunkSize;
if (ChunkSize > Constants.MaxChunkSize)
ChunkSize = Constants.MaxChunkSize;
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/102551/