Answered by:
FTP - Retrieve filesize from ftp server

Question
-
Hi everyone!
I'm working in a very simple updater for my application. This updater, connect to a FTP Server, checks the version of the major application and downloads it. I'm having some issues.
First, in order to give to the user some information about the download itself, I need a progress bar. To configure this progress bar I read a small file that contains the size of the file that will be downloaded(was the way I found to put it working quickly). I already found a way of getting a response with some data about the file, but I couldn't found a way to split the request response I've got:
Dim reqFTP As FtpWebRequest = Nothing reqFTP = DirectCast(WebRequest.Create("ftp://ftpserver/Filefolder/application.exe"), FtpWebRequest) reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails reqFTP.Credentials = New NetworkCredential("User", "password") Dim response = DirectCast(reqFTP.GetResponse(), FtpWebResponse) Dim responseStream As Stream = response.GetResponseStream() Dim reader As New StreamReader(responseStream) MsgBox(reader.ReadToEnd())
The response:
- -rw-r--r-- 1 App app 1508352 Jun 27 20:49 App.exe
The bold is the file size.
I don't know how to get only the file size from this response.
Then if it is possible to get the file size from this response, would exist something similar to the fileversion(I already have a text file to store the file version).
Very Thanks!
Valdirnm
Wednesday, July 2, 2014 11:37 PM
Answers
-
Well, thanks for the help.
I've done some tests and seems to me that the Ftp Server is not giving the answer to the WebRequestMethods.Ftp.GetFileSize request. I don't know if it is some configuration on that server.
As I wrote before, I have an answer if I do the WebRequestMethods.Ftp.ListDirectoryDetails.But the response I get is a string with the content:
- -rw-r--r-- 1 App app 1508352 Jun 27 20:49 App.exe
As we can see, the filesize apears, but I don't know how could I get only that part of the string.
I tried to use regex to isolate only the filesize information, but I couldn't understand a way of doing that.Could someone help on that?
Valdirnm
That doesn't make sense that you can get that information and not use the function that I showed, so I put another example together. It may be that you didn't configure something quite right to use it.
To start with, I put a file (a PDF) on an area of my website that I could test with as shown below:
Following is the code used to check the size (using FTP):
Option Strict On Option Explicit On Imports System.Net Public Class Form1 Private Const serverName As String = "ftp://fls-online.com/" Private Const userName As String = "testarea@fls-online.com" Private Const passWord As String = "#201,2CE)@MQ" Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim credential As New NetworkCredential(userName, passWord) Dim uri As New Uri(serverName & "A419.pdf") Dim testFileSize As Long = ReturnRemoteFileSize(uri, credential) MessageBox.Show(testFileSize.ToString("f0")) Stop End Sub ''' <summary> ''' This function will return a value indicating the size ''' (bytes) of a file on a remote server using FTP. ''' </summary> ''' <param name="uri">A URI for this particular server and filename.</param> ''' <param name="cred">Network credentials needed to log on.</param> ''' <returns></returns> ''' <remarks></remarks> Private Function ReturnRemoteFileSize(ByVal uri As Uri, _ ByVal cred As NetworkCredential) As Long Dim retVal As Long = 0 Try Dim thisFTP As FtpWebRequest = DirectCast(WebRequest.Create(uri), FtpWebRequest) With thisFTP .Credentials = cred .Method = WebRequestMethods.Ftp.GetFileSize Using ftpResponse As FtpWebResponse = DirectCast(thisFTP.GetResponse, FtpWebResponse) retVal = ftpResponse.ContentLength End Using End With Catch ex As Exception retVal = -1 End Try Return retVal End Function End Class
And following is the result:
Have another look and see if maybe you didn't set it up quite right?
Still lost in code, just at a little higher level.
:-)- Marked as answer by Governm Monday, July 7, 2014 2:34 PM
Sunday, July 6, 2014 2:51 PM -
Well, thanks for the help.
I've done some tests and seems to me that the Ftp Server is not giving the answer to the WebRequestMethods.Ftp.GetFileSize request. I don't know if it is some configuration on that server.
As I wrote before, I have an answer if I do the WebRequestMethods.Ftp.ListDirectoryDetails.But the response I get is a string with the content:
- -rw-r--r-- 1 App app 1508352 Jun 27 20:49 App.exe
As we can see, the filesize apears, but I don't know how could I get only that part of the string.
I tried to use regex to isolate only the filesize information, but I couldn't understand a way of doing that.Could someone help on that?
Valdirnm
This code assumes the bolded number will always be greater then 3 characters in length. Which for an .Exe I would suspect is true.
Although if the 1 before App is a counter for each file in the directory it could increment past 3 characters too.
Option Strict On Public Class Form1 Dim InfoForApp As String = "•-rw-r--r-- 1 App app 1508352 Jun 27 20:49 App.exe" Dim InfoForAppSplit() As String Dim Length As Integer = 0 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.CenterToScreen() End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click InfoForAppSplit = InfoForApp.Split(" "c) For i = 0 To InfoForAppSplit.Count - 1 If InfoForAppSplit(i).Count > 3 And IsNumeric(InfoForAppSplit(i)) Then Label1.Text = InfoForAppSplit(i) Length = CInt(Label1.Text) Me.Text = Length.ToString Exit For End If Next End Sub End Class
La vida loca
- Edited by Mr. Monkeyboy Monday, July 7, 2014 2:33 AM
- Marked as answer by Governm Monday, July 7, 2014 2:34 PM
Monday, July 7, 2014 2:31 AM
All replies
-
I would suspect the project or browsable code for the project at the above link could do that.
It uses httpwebrequest instead of ftpwebrequest though.
Google search for "FTPWebRequest download with progress bar vb.net"
La vida loca
Wednesday, July 2, 2014 11:48 PM -
See if this might help:
''' <summary> ''' This function will return a value indicating the size (bytes) of a file on a remote server. Note this is an HTTP function, not FTP. ''' </summary> ''' <param name="url">The full URL of the file.</param> ''' <returns></returns> ''' <remarks>Not all servers support this function, so a return value of -1 doesn't necessarily mean it's not there.</remarks> Private Function ReturnRemoteFileSize(ByVal url As String) As Long Dim retVal As Long = 0 Try Dim req As System.Net.WebRequest = System.Net.HttpWebRequest.Create(url) req.Method = "HEAD" Using resp As WebResponse = req.GetResponse Dim remoteFileSize As Long = resp.ContentLength retVal = remoteFileSize End Using Catch ex As Exception retVal = -1 End Try Return retVal End Function ''' <summary> ''' This function will return a value indicating the size (bytes) of a file on a remote server using FTP. ''' </summary> ''' <param name="uri">A URI for this particular server and filename.</param> ''' <param name="cred">Network credentials needed to log on.</param> ''' <returns></returns> ''' <remarks></remarks> Private Function ReturnRemoteFileSize(ByVal uri As Uri, ByVal cred As NetworkCredential) As Long Dim retVal As Long = 0 Try Dim thisFTP As FtpWebRequest = DirectCast(WebRequest.Create(uri), FtpWebRequest) With thisFTP .Credentials = cred .Method = WebRequestMethods.Ftp.GetFileSize Using ftpResponse As FtpWebResponse = DirectCast(thisFTP.GetResponse, FtpWebResponse) retVal = ftpResponse.ContentLength End Using End With Catch ex As Exception retVal = -1 End Try Return retVal End Function
The HTTP method is a lot faster than FTP (no logging in required), but use whichever one is appropriate for what you're doing.
I hope that helps. :)
Still lost in code, just at a little higher level.
:-)- Proposed as answer by Franklin ChenMicrosoft employee Thursday, July 3, 2014 7:57 AM
Thursday, July 3, 2014 12:25 AM -
Well, thanks for the help.
I've done some tests and seems to me that the Ftp Server is not giving the answer to the WebRequestMethods.Ftp.GetFileSize request. I don't know if it is some configuration on that server.
As I wrote before, I have an answer if I do the WebRequestMethods.Ftp.ListDirectoryDetails.But the response I get is a string with the content:
- -rw-r--r-- 1 App app 1508352 Jun 27 20:49 App.exe
As we can see, the filesize apears, but I don't know how could I get only that part of the string.
I tried to use regex to isolate only the filesize information, but I couldn't understand a way of doing that.Could someone help on that?
Valdirnm
- Edited by Governm Sunday, July 6, 2014 11:10 AM Best view
Sunday, July 6, 2014 11:09 AM -
Well, thanks for the help.
I've done some tests and seems to me that the Ftp Server is not giving the answer to the WebRequestMethods.Ftp.GetFileSize request. I don't know if it is some configuration on that server.
As I wrote before, I have an answer if I do the WebRequestMethods.Ftp.ListDirectoryDetails.But the response I get is a string with the content:
- -rw-r--r-- 1 App app 1508352 Jun 27 20:49 App.exe
As we can see, the filesize apears, but I don't know how could I get only that part of the string.
I tried to use regex to isolate only the filesize information, but I couldn't understand a way of doing that.Could someone help on that?
Valdirnm
That doesn't make sense that you can get that information and not use the function that I showed, so I put another example together. It may be that you didn't configure something quite right to use it.
To start with, I put a file (a PDF) on an area of my website that I could test with as shown below:
Following is the code used to check the size (using FTP):
Option Strict On Option Explicit On Imports System.Net Public Class Form1 Private Const serverName As String = "ftp://fls-online.com/" Private Const userName As String = "testarea@fls-online.com" Private Const passWord As String = "#201,2CE)@MQ" Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim credential As New NetworkCredential(userName, passWord) Dim uri As New Uri(serverName & "A419.pdf") Dim testFileSize As Long = ReturnRemoteFileSize(uri, credential) MessageBox.Show(testFileSize.ToString("f0")) Stop End Sub ''' <summary> ''' This function will return a value indicating the size ''' (bytes) of a file on a remote server using FTP. ''' </summary> ''' <param name="uri">A URI for this particular server and filename.</param> ''' <param name="cred">Network credentials needed to log on.</param> ''' <returns></returns> ''' <remarks></remarks> Private Function ReturnRemoteFileSize(ByVal uri As Uri, _ ByVal cred As NetworkCredential) As Long Dim retVal As Long = 0 Try Dim thisFTP As FtpWebRequest = DirectCast(WebRequest.Create(uri), FtpWebRequest) With thisFTP .Credentials = cred .Method = WebRequestMethods.Ftp.GetFileSize Using ftpResponse As FtpWebResponse = DirectCast(thisFTP.GetResponse, FtpWebResponse) retVal = ftpResponse.ContentLength End Using End With Catch ex As Exception retVal = -1 End Try Return retVal End Function End Class
And following is the result:
Have another look and see if maybe you didn't set it up quite right?
Still lost in code, just at a little higher level.
:-)- Marked as answer by Governm Monday, July 7, 2014 2:34 PM
Sunday, July 6, 2014 2:51 PM -
Well, thanks for the help.
I've done some tests and seems to me that the Ftp Server is not giving the answer to the WebRequestMethods.Ftp.GetFileSize request. I don't know if it is some configuration on that server.
As I wrote before, I have an answer if I do the WebRequestMethods.Ftp.ListDirectoryDetails.But the response I get is a string with the content:
- -rw-r--r-- 1 App app 1508352 Jun 27 20:49 App.exe
As we can see, the filesize apears, but I don't know how could I get only that part of the string.
I tried to use regex to isolate only the filesize information, but I couldn't understand a way of doing that.Could someone help on that?
Valdirnm
This code assumes the bolded number will always be greater then 3 characters in length. Which for an .Exe I would suspect is true.
Although if the 1 before App is a counter for each file in the directory it could increment past 3 characters too.
Option Strict On Public Class Form1 Dim InfoForApp As String = "•-rw-r--r-- 1 App app 1508352 Jun 27 20:49 App.exe" Dim InfoForAppSplit() As String Dim Length As Integer = 0 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.CenterToScreen() End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click InfoForAppSplit = InfoForApp.Split(" "c) For i = 0 To InfoForAppSplit.Count - 1 If InfoForAppSplit(i).Count > 3 And IsNumeric(InfoForAppSplit(i)) Then Label1.Text = InfoForAppSplit(i) Length = CInt(Label1.Text) Me.Text = Length.ToString Exit For End If Next End Sub End Class
La vida loca
- Edited by Mr. Monkeyboy Monday, July 7, 2014 2:33 AM
- Marked as answer by Governm Monday, July 7, 2014 2:34 PM
Monday, July 7, 2014 2:31 AM -
Very thanks for all the help!! :)
I realized that the WebRequestMethods.Ftp.GetFileSize problem was really in the FTP Server. (I've created a temporary one and tested. The code works perfectly. I remembered too that is possible the FTP Server being Linux. Maybe there is a configuration or option creating the problem. Or who knows it is some kind of delay).
Anyway, I will keep using the WebRequestMethods.Ftp.ListDirectoryDetails. I understand that it is not the best solution, but it is what is working for the FTP Server right now. And I will use the code for getting the file size from the string response.
THANKS FOR THE AMAZING HELP!!!
Ps.: I will learn more about the HTTP request and who knows make life easier :)
Valdirnm
Monday, July 7, 2014 2:43 PM