C# getting file size of an FTP file in a while loop
I am writing a program which lets me easily upload a file (drag file over exe) to my FTP server.
While the upload is in progress, I am trying to get the already uploaded file size in another thread.
This works exactly 2 times (caching?), then it just stops giving me feedback of the actual file size on the FTP server.
System.Net.WebRequest req = System.Net.HttpWebRequest.Create(new Uri("http://drop.buto.ch/" + file));
req.Method = "HEAD";
System.Net.WebResponse re开发者_开发技巧sp = req.GetResponse();
int ContentLength;
if (int.TryParse(resp.Headers.Get("Content-Length"), out ContentLength))
{
return ContentLength;
}
or this
System.Net.WebClient wc = new System.Net.WebClient();
wc.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
wc.Headers[HttpRequestHeader.CacheControl] = "no-cache";
wc.OpenRead(new Uri("http://drop.buto.ch/" + file));
Int64 bytes_total = Convert.ToInt32(wc.ResponseHeaders["Content-Length"]);
return bytes_total;
I have also tried achieving this with WebRequestMethods.Ftp.GetFileSize
but this wouldn't let me access the file in Binary mode, instead its going over the ASCII way which is returning me an Error (550).
How can I get the file size on the FTP more than two times?
There is a good example of performing the ftp transfer and getting it's ongoing progress (assuming that is what you are trying to achieve) at the following address:
http://tim.mackey.ie/FtpWebRequestWithProgressBarCFtp.aspx
It will most likely give better results than attempting to get the partial files content size through a http server.
精彩评论