Ftp upload - C# (235341)
Формулировка задачи:
Пытаюсь загрузить файл на сервер с ограничением скорости. Вот код:
Файл грузится, но без ограничений.
ThrottledStream взял отсюда http://www.codeproject.com/Articles/18243/Bandwidth-throttling
Добавил ещё 1 запись файла и Stopwatch, заливается столько, сколько нужно. Убираю запись опять на максимальной скорости закачка идёт. И ещё. В первый запуск sw 0 показывает.
Работает.
Не работает(((
Как с этим sw связан?
Буду признателен, если кто-то объяснит почему эта хрень работает только после слипа. Пробовал ещё Sleep(1), тоже не пашет.
Stream stream = request.GetRequestStream();
Stream throttle = new ThrottledStream(stream, 10);
throttle.Write(fileContents, 0, fileContents.Length);
return true;
Прям загадка какая-то.
public static bool FtpUpload(string uri, string file)
{
Stopwatch sw = new Stopwatch();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri + file);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.Credentials = new NetworkCredential("", "");
byte[] fileContents = File.ReadAllBytes(file.Replace("|", null));
File.Delete(file.Replace("|", null));
request.ContentLength = fileContents.Length;
Stream stream = request.GetRequestStream();
long bytes = 5000;
Stream throttle = new launcher.ThrottledStream(stream, bytes);
sw.Start();
stream.Write(fileContents, 0, fileContents.Length);
sw.Stop();
MessageBox.Show(sw.ElapsedMilliseconds.ToString());
sw.Reset();
sw.Start();
throttle.Write(fileContents, 0, fileContents.Length);
sw.Stop();
MessageBox.Show(sw.ElapsedMilliseconds.ToString());
return true;
} Stopwatch sw = new Stopwatch();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri + file);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.Credentials = new NetworkCredential("", "");
byte[] fileContents = File.ReadAllBytes(file.Replace("|", null));
File.Delete(file.Replace("|", null));
request.ContentLength = fileContents.Length;
Stream stream = request.GetRequestStream();
long bytes = 5000;
Stream throttle = new launcher.ThrottledStream(stream, bytes);
sw.Start();
// stream.Write(fileContents, 0, fileContents.Length);
sw.Stop();
MessageBox.Show(sw.ElapsedMilliseconds.ToString());
sw.Reset();
sw.Start();
throttle.Write(fileContents, 0, fileContents.Length);
sw.Stop();
MessageBox.Show(sw.ElapsedMilliseconds.ToString());
return true; Stopwatch sw = new Stopwatch();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri + file);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.Credentials = new NetworkCredential("", "");
byte[] fileContents = File.ReadAllBytes(file.Replace("|", null));
File.Delete(file.Replace("|", null));
request.ContentLength = fileContents.Length;
Stream stream = request.GetRequestStream();
long bytes = 5000;
Stream throttle = new launcher.ThrottledStream(stream, bytes);
throttle.Write(fileContents, 0, fileContents.Length);
return true;
М... Какое-то странное решение нашлось.
Stopwatch sw = new Stopwatch();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri + file);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.Credentials = new NetworkCredential("", "");
byte[] fileContents = File.ReadAllBytes(file.Replace("|", null));
File.Delete(file.Replace("|", null));
request.ContentLength = fileContents.Length;
Stream stream = request.GetRequestStream();
long bytes = 3000;
Stream throttle = new launcher.ThrottledStream(stream, bytes);
Thread.Sleep(10);
throttle.Write(fileContents, 0, fileContents.Length);
return true;
Походу ни то, ни другое не пашет. Просто пауза выставляется а потом сразу весь файл заливается. хээээлп(
Решение задачи: «Ftp upload»
textual
Листинг программы
ThrottledStream str = new ThrottledStream(request.GetRequestStream(), 5 * 1024); str.Write(buff, 0, buff.Length); str.Close();