Jump to content

C# checking filesize with Webclient?

Flux Azreal

I'm trying to check the file size of a file in a FTP server but it keeps giving me "0"

i'm not sure why i try it on other things aswell some work some down 

like on this it won't ftp://speedtest.tele2.net/100MB.zip

but on some it works like this one https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwiR6-3r-7XjAhWEm-AKHedzADcQjRx6BAgBEAU&url=https%3A%2F%2Fwww.google.com%2Fimghp%3Fhl%3Den&psig=AOvVaw3EjuDLjwhqXdoxKhKg9Jl4&ust=1563247219877862

 

could this be because of a server setting? (using filezilla to host ftp server)

 

var client = new WebClient();
client.OpenRead("direct link");
Int64 bytes_total = Convert.ToInt64(client.ResponseHeaders["Content-Length"]) / 1024;
Console.WriteLine(bytes_total.ToString());

 

Link to comment
Share on other sites

Link to post
Share on other sites

I would go with an actual FTP request with the FileSize method directly instead of trying to reinvent the wheel

 

// create the ftp request object
var ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://serveradress"));

// put credential if you have
ftpRequest.Credentials = new NetworkCredential("user", "pass");

// here's the actual important part. We ask for the method to return the file size
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;

// make the actual request and get the response
var response = (FtpWebResponse)ftpRequest.GetResponse();

// store the resposne length which is the value we want
var size = response.ContentLength;

// close the connection
response.Close();

 

This will not actually open or iterate the file bytes as a WebClient will. But you have to make sure your FTP supports the feature SIZE. Most major one does by default (Apache, IIS)

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, Franck said:

I would go with an actual FTP request with the FileSize method directly instead of trying to reinvent the wheel

 


// create the ftp request object
var ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://serveradress"));

// put credential if you have
ftpRequest.Credentials = new NetworkCredential("user", "pass");

// here's the actual important part. We ask for the method to return the file size
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;

// make the actual request and get the response
var response = (FtpWebResponse)ftpRequest.GetResponse();

// store the resposne length which is the value we want
var size = response.ContentLength;

// close the connection
response.Close();

 

This will not actually open or iterate the file bytes as a WebClient will. But you have to make sure your FTP supports the feature SIZE. Most major one does by default (Apache, IIS)

Then how would I properly do it?

Link to comment
Share on other sites

Link to post
Share on other sites

57 minutes ago, Flux Azreal said:

Then how would I properly do it?

did you try my code ? did it not work ? if it didn't work you need to enable SIZE on your FTP server that refer to your manual.

Link to comment
Share on other sites

Link to post
Share on other sites

On 7/15/2019 at 6:21 AM, Flux Azreal said:

I'm trying to check the file size of a file in a FTP server but it keeps giving me "0"

i'm not sure why i try it on other things aswell some work some down 

like on this it won't ftp://speedtest.tele2.net/100MB.zip

but on some it works like this one https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwiR6-3r-7XjAhWEm-AKHedzADcQjRx6BAgBEAU&url=https%3A%2F%2Fwww.google.com%2Fimghp%3Fhl%3Den&psig=AOvVaw3EjuDLjwhqXdoxKhKg9Jl4&ust=1563247219877862

 

could this be because of a server setting? (using filezilla to host ftp server)

 


var client = new WebClient();
client.OpenRead("direct link");
Int64 bytes_total = Convert.ToInt64(client.ResponseHeaders["Content-Length"]) / 1024;
Console.WriteLine(bytes_total.ToString());

 

The "Content-Length is a response header that is something typical of HTTP and HTTPS ... it's part of the protocol.

You make a request to the web server (this is done transparently for you by the Web Client when you call open read)

The server sends you the response header (several lines, followed by the actual content) ... here's an example of a response header:

 

Accept-Ranges : bytes
Content-Length : 487
Content-Type : image/png
Date : Tue, 16 Jul 2019 09:25:59 GMT
Last-Modified : Sun, 30 Jun 2019 08:08:21 GMT
Server: Apache

And you're getting the file size by reading the content-length response.

 

FTP is a different protocol, it doesn't have response headers.

You send a command and the ftp server sends a reply. If you send a command to get a file, the ftp server replies with a status message and optionally with the ip and port from where you can retrieve the file (the data is separate connection from the connection used for commands)

So you can simply say  RETR /some_folder/filename.zip  and the server simply says  200 OK, file is available on IP 1.2.3.4 port 1234   and you connect to 1.2.3.4:1234 and you start receiving bytes... so at no point you get a content-length or any other response header.

The way you would have to do it is to use the CWD command (change working directory) to make sure the folder actually exists, then you retrieve the list of files from the folder using LIST and then you parse the list and get the file size from that. Newer ftp servers support SIZE command, so you could say SIZE /some_folder/filename.zip  and you get a number back. 

Then you would actually use RETR to retrieve the file.

The WebLink simply calls RETR without bothering to ask for file size or list the folder contents to retrieve the file size, last modified time, file attributes.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×