B4J Tutorial [ABMaterial] Downloading a file from the server. My example

Hello.

Sorry for my English.

I've been struggling with the problem of sending any file from the server to the client from the ABMaterial. In the end, I did it and it works. It may be useful to someone in their applications. My example can be applied to the list, a button, an image, etc.

Principle of operation:

The Main plants variable (example folder)
B4X:
Public folder_file As String = "\send_files\"
The folder where the files to be sent must be in the structure of /www/name_webapp/

Add server hadler in Main
B4X:
srvr.AddHandler ( "/send", "send", True)

The code for 'send'
B4X:
'Handler class
sub Class_Globals
End Sub

Public Sub Initialize
End Sub

Sub Handle (As ServletRequest req, resp As ServletResponse)
    Dim FileName As String = req.GetParameter ( "file")
Dim Folder As String = File.DirApp & "\www\" & ABMShared.AppName & Main.folder_file
'Log (folder & FileName)
If File.Exists (folder, FileName) = True Then
   In Dim As InputStream = File.OpenInput (folder, FileName)
   resp.SetHeader ( "Content-Type", "application/xxx")
   resp.SetHeader ( "Content-disposition", "filename="""&FileName&"""")
   File.Copy2 (In, resp.OutputStream)
   else
resp.Write ( "File not found. Sorry.")
End If
End Sub

Calling for such a button.

B4X:
Sub btn_Clicked (target As String)

Dim namefile As ABMInput = page.Component ( "inp")
ABMShared.NavigateToPage (ws, "send", "../../send?File ="&Namefile.Text&"&session=")

End Sub

On pressing the button, you are taken in the name of the file and we ABMInput link
B4X:
http://name_server/name_webapp/send?file=name_file&session=number_session
This way you can send any file of any size to the customer.
The code is for Windows, Linux must blizzard '\' to '/' variable 'folder' and 'folder_file'
Written in ABMaterial 2.20

Link to source DEMO (my private cloud)
 
Top