B4J Question [ABMaterial] manage attachments

rbirago

Active Member
Licensed User
Longtime User
In a forum like this it is normal to upload files (images, pdf documents, etc.) to share with others. At the same time in this forum I can click on an attached file to show it or to download it. That's normal!
If I want to reach the same targets using ABMaterial for now I have found some solutions:
To show an image, pdf or also a video I can use an ABMLabel (see this old thread) in a very straight way.
To upload a file there is an ABMUpload that solve the task very well.
At the opposite I have not found an easy way (similar to the "show" task) to download a file. I know that there is a very complete ABMFileManager that manage in many ways this and other tasks, but I need something easier (using the standard capabilities of the browser).
At last a different question: how to link to an external static url by code (e.g. by a menu item or a button click instead of an ABMLabel)?
Thank you
Roberto
 

alwaysbusy

Expert
Licensed User
Longtime User
You will need to use some javascript for that and find out the real ID from the clickable tag. This is for example how I let the user download a generated CSV, XLS or XLSX file I generated with the jPoi library in the menu:

B4X:
' the clickable IDs are #csv, #xls and #xlsx but they may be more complex in your case. Use the Chrome Dev Tools (F12 and inspect the element to find out what it is)
' I generate the files with a GUID filename and use download= to give it a proper name for the user (Export with the timestamp in my case).
page.ws.Eval($"$('#csv').html("<a href='../../ExportTemplates/Export_${GUID}.csv' download='Export ${NowDate}.csv' class='white-text waves-effect '>CSV</a>");"$, Null)
page.ws.Eval($"$('#xls').html("<a href='../../ExportTemplates/Export_${GUID}.xls' download='Export ${NowDate}.xls' class='white-text waves-effect '>Excel XLS</a>");"$, Null)
page.ws.Eval($"$('#xlsx').html("<a href='../../ExportTemplates/Export_${GUID}.xlsx' download='Export ${NowDate}.xlsx' class='white-text waves-effect '>Excel XLSX</a>");"$, Null)
page.ws.Flush

Note that the files Url always MUST be within the /www folder e.g. in my case it is in the /www/ExportTemplates/ folder.

Alwaysbusy
 
Upvote 0

rbirago

Active Member
Licensed User
Longtime User
Uhmm...perhaps I have not well explained my objective:
Let's suppose I want to make downloadable my file www/Exports/MyExcel.xls clicking on ABMButton (id="mybutton").
I have to give the commands in the mybutton_clicked(...) sub.
I was thinking of something similar to {AL}...{AT}...{/AL} way used to show an image or similar in the Initialize of a clickable ABMLabel.
My doubt is in retrieving the id directly from Chrome. Why from Chrome and not the internal id? is it not the same id I have put in the button? what if a user uses a different browser?
...and at last the second question: which are the instructions triggered by a button click if I want to jump for example to the static www.MySite.HelpPage.html ?
Thanks for your patience
Roberto
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Why from Chrome and not the internal id?
because the id you give may be changed to something else as an id in html needs to be unique. So if suppose you have a button in a container, the id may be 'cont-btn1'.
Ids will be the same across browers.

To go to another site (and for your download in the button), you can try this (untested):
B4X:
ws.Eval("window.open(arguments[0],'_blank')", Array As Object(TargetUrl))
ws.flush

Note that for your document, you will have to give it the full url (http://mysite.com/myproject/Exports/MyExcel.xls or something like this)

The advantage of my previous example is that it can do a force download. Not sure if this one can.

Alwaysbusy
 
Upvote 0

rbirago

Active Member
Licensed User
Longtime User
Link to another site works like a breeze.
Later I will try the download.
Thank you
Roberto
 
Upvote 0

rbirago

Active Member
Licensed User
Longtime User
Perhaps the Download Task is easier:
If I use the ABMLabel initialize using {AL}...{AT}...{/AL} method the browser shows my document in a new page...and after I can save the document right-clicking on it.
...and if the document is for example a .xls or another non-displayable document the browser downloads it directly!!!
Really simple. I suggest it as a good solution.
Roberto
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Perhaps the Download Task is easier:
If I use the ABMLabel initialize using {AL}...{AT}...{/AL} method the browser shows my document in a new page...and after I can save the document right-clicking on it.
...and if the document is for example a .xls or another non-displayable document the browser downloads it directly!!!
Really simple. I suggest it as a good solution.
Roberto
That is how I handle it. Works nice...

I also use ABMChip effectively as well... (using AddArrayComponent )

B4X:
        Dim SQL As SQL = DBM.GetSQL
        Dim SQL_str As String = "SELECT * FROM caseattachments WHERE CaseACaseID = " & ActDetid &" AND CaseAType > 1"
        Dim attachs As List = DBM.SQLSelect(SQL,SQL_str, Null)
        Log(" attach case id: "&attachs.Size)

      Dim attLocation As String =  "../uploads/" &"comp_"&Maincomp_id&"/" ' files are stored in company named folders...
                
        For i=0 To attachs.Size - 1
            Dim attach As Map = attachs.Get(i)

            Dim CaseAID As String  =  attach.Get("caseavalue")
            
            Dim filename As String = attach.Get("shortname")
            Attachments.Put(filename, CaseAID)   
            
            Dim chip As ABMChip
            attCounter = attCounter + 1
            Dim casetype As Int = attach.Get("caseatype")
            
            If casetype = 0 Then
                chip.Initialize(page,  ""&attCounter, "{AL}" & attLocation & CaseAID & "{AT}{C:#000000}" &filename & "{/C}{/AL}" , True, "chip")
            Else
                chip.Initialize(page,  ""&attCounter, "{AL}" &  CaseAID & "{AT}{C:#000000}" &filename & "{/C}{/AL}" , True, "chip")
            End If   
            chip.Tag = filename
            inp.Content.Cell(5,1).AddArrayComponent(chip, "Chiplnkcase")               
        Next   
        DBM.CloseSQL(SQL)
 
Upvote 0
Top