B4J Question [ABMaterial] / Jetty how to download an xlsx file.

ToolboxZX

Member
Licensed User
Longtime User
I'm not exactly sure which category this question falls into (ABMaterial or simply the Jetty web server) as it impacts both.

Consider the following code. It is a slight variant from what Mashiane originally posted to download an excel file and works perfect for sending an excel file with an "xls" extension:
B4X:
page.Pause
'Excel Report developed in this area
       'Report generation code goes here
'Excel Report development complete.  

DateTime.DateFormat= "yyyy-MM-dd"
    DateTime.TimeFormat="HHmm"
  
    Dim stDateTime As String ="_" & DateTime.Date(DateTime.Now) & "_" & DateTime.Time(DateTime.now)
    Dim outFileName As String = "Inventory_" & stDateTime & ".xls"
    'save the file in the reports folder
    Dim fPath As String = File.Combine(File.DirApp,"www/" & ABMShared.AppName.tolowercase & "/reports")
    wb.Save(fPath, outFileName)
    wb.Close
    page.resume
    ' we want to execute a download of the file, open the document in a new browser tab
    Dim pgLink As String = "../reports/" & outFileName
    If ws.Open Then
      
        'force a download of the document
        ws.Eval("window.open(arguments[0],'_blank');", Array As Object(pgLink ))


         ws.Flush
    End If

If I change the line of code with the extension to xlsx:
B4X:
Dim outFileName As String = "Inventory_" & stDateTime & ".xlsx"

Instead of the file download prompt, the browser attempts to open the document in the browser. :(

I have a suspicion that it is caused by Jetty not knowing the MIME type for xlsx.

So my question is:

Is there a way to add this MIME type to Jetty, or is it possible to pass this parameter in the javascript line:
B4X:
ws.Eval("window.open(arguments[0],'_blank');", Array As Object(pgLink ))

I know I could also achieve the same thing by adding a sending handler, or simply zipping up the excel file (as "zip" is also understood).

But wanted to know if MIME addition or javascript line modification would achieve the same results. Thank you.
 

Harris

Expert
Licensed User
Longtime User
Golly gosh dang...

I just fought this for many hours...

B4X:
Public Sub FindHelp(ws As WebSocket,  pagename As String) As Boolean
    Dim ret As Boolean = False
    Dim SQL As SQL = DBM.GetSQL
    Dim hlpf As List = DBM.SQLSelect(SQL, "Select * From helpfile where pagename = '"&pagename&"'", Null)
    If hlpf.Size > 0 Then
        Dim tbl As Map = hlpf.Get(0)
        Dim fname As String = tbl.Get("helpname")
        If ws.Open Then
                ' "_blank" is default (new tab opened)
                ws.Eval( "window.open( '../"&fname&"')", Null) ' here is a problem.... this can raise a blocking of new pop-ups in many browsers..
                ws.Flush
                ret = True
        End If   
    End If
    DBM.CloseSQL(SQL)
    Return ret
End Sub

window.open (on many browsers) will block this call (pop-up) UNTIL the user accepts this site as safe.
Often, you don't even see the block happening - it happens too quick.. (ie. Chrome)

Here is my page with help TRYING to explain this situation - and resolve it.

popup.jpg



window.open was the cause of this (it blocks until user accepts). window.location = "whatever" did not raise this issue, yet it opens the (object) in the SAME tab (I wanted an new tab).

fun and games...
 
Upvote 0
Top