B4J Question [SOLVED] [ABMaterial] ABMFileInput and DownloadFolder

giannimaione

Well-Known Member
Licensed User
Longtime User
hello,
ABMFileInput works fine,
B4X:
Sub Class_Globals
Public DownloadFolder As String = "/www/" & ABMShared.AppName & "/uploads/"
....
....
End Sub
but it's possibile change AT RUNTIME folder destination in DownloadFolder ???
my www structure:
www
-> appname
--> uploads
---> pdfFolder
---> docFolder
---> other file
 

Harris

Expert
Licensed User
Longtime User
In these examples, users that login belong to a company (ID).
Instead of grouping everyone's files into one folder, new folders are created based on the company ID and the file type - at runtime.

This makes cleanup and viewing much easier since a user can only access their files.

Based the the desired file's extension (.doc, pdf, jpg, etc) - make the determination of where to store OR retrieve it...

B4X:
' the location of attachment files
Dim attLocation As String =  "../uploads/" &"comp_"&Maincomp_id&"/"

' or

'  Next Reports needs a place to write generated PDF report files

Maincomp_id = session.GetAttribute2("comp_id", 0) ' set the local "company" variable based on session variable
pdfFolder = File.DirApp &"/www/"& ABMShared.AppName&"/rptout_"&Maincomp_id
File.MakeDir(pdfFolder, "rptout_"&Maincomp_id)


' cleanup old PDF files - no longer needed...

    Dim mylist As List
    mylist.Initialize
    
    mylist = File.ListFiles(pdfFolder)
    If mylist.Size > 0 Then
        For i = 0 To mylist.Size-1
            If File.LastModified(pdfFolder,mylist.Get(i) ) < DateTime.Now - (DateTime.TicksPerDay * 10) Then
                File.Delete(pdfFolder,mylist.Get(i))
                Log("deleting: "&mylist.Get(i))
            Else
'                Log("NOT deleting: "&mylist.Get(i))
                    
            End If
        Next
    End If


' upload handler....
Sub Handle(req As ServletRequest, resp As ServletResponse)    
    'get the callback page from the session (multiple modules can use this handler)
    Dim callback As Object = req.GetSession.GetAttribute("abmcallback")    
'    Dim downloadfolder As String = File.Combine(File.DirApp, req.GetSession.GetAttribute("abmdownloadfolder"))
    Maincomp_id = req.GetSession.GetAttribute2( "comp_id", 0)
    Dim downloadfolder As String = File.Combine(File.DirApp, "/www/" & ABMShared.AppName & "/uploads/"&"comp_"&Maincomp_id)
    If Maincomp_id = 0 Then
        Log ("--- ERROR --- MAINCOMPID IS 0 IN UPLOAD HANDLER: "&Maincomp_id)
    Else
        Log ("--- GOOD!!! --- MAINCOMPID IS CORRECT UPLOAD HANDLER: "&Maincomp_id)
            
    End If
 
Upvote 1

Harris

Expert
Licensed User
Longtime User
Please set title to [Solved] when completed - with preferably an example of your implementation of the issue.
We love to see how others handle these diverse situations... Gives us all food for thought along with new insight....

Thanks
 
Upvote 0

giannimaione

Well-Known Member
Licensed User
Longtime User
[solved]
here my code
into mypage "ABM Web Page"
B4X:
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
.....
.....
ws.Session.SetAttribute("abmcallback", Me)
ws.Session.SetAttribute("abmdownloadfolder", DownloadFolder)
ws.Session.SetAttribute("abmmaxsize", DownloadMaxSize)
ws.Session.SetAttribute("abmtargetfolder", "") '<------ new
...
...
End Sub
'
'
Sub gmfileinput_Changed(value As String)
....
....
ws.Session.SetAttribute("abmtargetfolder", "folder" & subFolder ) '<------ new
file2Upload.UploadToServer 'Here the file is uploading
file2Upload.Clear
End Sub

and after into module "ABMUploadHandler"

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
Dim TargetFolder As String = req.GetSession.GetAttribute("abmtargetfolder") '<---- new
If File.Exists (DownloadFolder & "/" & TargetFolder, TargetFolder) = False Then
            Log("make folder for each users")
            File.MakeDir(DownloadFolder, TargetFolder)
            File.OpenOutput(DownloadFolder & "/" & TargetFolder, TargetFolder, True)
            TargetFolder = DownloadFolder & "/" & TargetFolder
End If
DownloadFolder = TargetFolder
End Sub
thanks @Harris
 
Upvote 0
Top