Android Question Reads the date of a remote file

AlpVir

Well-Known Member
Licensed User
Longtime User
This code reads the date of a remote file :

B4X:
Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
   Dim cs As CountingOutputStream
   cs.Initialize(File.OpenOutput(TempFolder, TaskId, False))
   Dim j As HttpJob = TaskIdToJob.Get(TaskId)
   Dim jt As JobTag = j.Tag
   jt.CountingStream = cs
   jt.Total = Response.ContentLength
   If jt.Data.url = "" Then
   Log("Job cancelled before downloaded started")
      cs.Close
   End If
   Response.GetAsynchronously("response", cs , True, TaskId)
   '---
   Dim Headers         As Map=Response.GetHeaders
   Dim i            As Int
   Dim Key, Value      As String
   For i=0 To Headers.Size-1
      Key=Headers.GetKeyAt(i)
      if Key="last-modified" Then
         value=Headers.GetValueAt(i)
         If Response.ContentType="image/jpeg" Then
              '*****************
               strRemoteData=Value.Trim
              '*****************
         End If
      End If
   Next
End Sub

Unfortunately you must first download the entire download.
It can be very slow in the case of large files.
Is there a way to download only the first 100 bytes (for example) of the file (where is the header being stored, I suppose) ?
Thanks in advance.
 

DonManfred

Expert
Licensed User
Longtime User
If the file is stored on your server then you can use a B4J Server app to return the Date.
Or an small php-script.

This php returns the image-dimensions. But it can be easily adapted to return a filedate.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Another option (to be tested): if files are reachable through FTP than you can issue a LIST command to the FP server for the directory where the file you are interested reside in. This should give you some extra info beside the file names (file size, file last modification date..). AFAIK, it all depends on how the LIST command is programmed on the server though.

udg
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
The most ductile solution I think is that of a dynamic page (ASP or PHP)
This is the ASP page
B4X:
<script language="vbscript" runat="server">
NomeFile=request("NomeFile")&".jpg"
dim Fso
dim f
set fso=Server.CreateObject("Scripting.FileSystemObject")
strFile=Server.MapPath("/Pano/" & NomeFile)
set f=fso.GetFile(strFile)
D=f.DateLastModified
response.write D
set f=nothing
set fso=nothing
</script>
From B4a side :
B4X:
job5.Initialize("Job5", Me)
job5.PostString("http://www........../LeggiData.asp", "NomeFile=" & NomeFile)
and
B4X:
Sub JobDone (Job As HttpJob)
   Dim D       As String
   If Job.Success = True Then
     Select Job.JobName
       Case "Job5"
         D=Job.GetString
         lbl.Text = "Data = " & D
     End Select
   Else
     Log("Error: " & Job.ErrorMessage)
     ToastMessageShow("Error: " & Job.ErrorMessage, True)
   End If
   Job.Release
End Sub
 
Last edited:
Upvote 0

Pravin Shah

Member
Licensed User
Longtime User
As mentioned by @udg, if the file is stored on the server then you can use FTP to list the files and get the last modified date of the file. I have provided the code which I have used in one of the app. I have used Service for the same. Hope you may find it useful.
The following code will list the files in images directory.
B4X:
Sub Service_Start (StartingIntent As Intent)
    'check for internet connection
        If Common.CheckConnection = False Then
        Return
    End If
   
    FileMap.Initialize
    Dim FTPIP As String = "xx.xx.xx.xxx"
    Dim FTPUserName As String = "xxxxxxx"
    Dim FTPPassword As String = "xxxxxx"
   
    FTP.Initialize("FTP", FTPIP, 21, FTPUserName, FTPPassword)
    FTP.PassiveMode = True
    FTP.List("\Images")       
End Sub

In the FTP_ListCompleted event you can capture the file attributes. I have provided example code below

B4X:
Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
    Log(ServerPath)
    Dim link As String
    Dim ServerFileSize, LocalFileSize As Long
    If Success = False Then
        Log(LastException)
    Else
        For i = 0 To Files.Length - 1
            Log(Files(i).Name & ", " & Files(i).Size & ", " & DateTime.Date(Files(i).Timestamp) & "--"&DateTime.Time(Files(i).Timestamp))
            FileWithExt = Files(i).Name
        next   
     end if
end sub
 
Upvote 0
Top