Januar Chayadi
Member
Hi there,
Im using ABMaterial to create web, running on my raspberry server.
I'm Trying to upload file from my laptop to server using ABMupload component, i only following tutorial on ABM website.
but somehow, the file is not uploaded.
here is my code. Btw in this code i'm using ABMupload and ABMFileInput. Both of that component have same problem (cannot upload to server )
Im using ABMaterial to create web, running on my raspberry server.
I'm Trying to upload file from my laptop to server using ABMupload component, i only following tutorial on ABM website.
but somehow, the file is not uploaded.
here is my code. Btw in this code i'm using ABMupload and ABMFileInput. Both of that component have same problem (cannot upload to server )
B4X:
'Class module
Sub Class_Globals
Private ws As WebSocket 'ignore
' will hold our page information
Public page As ABMPage
' page theme
Private theme As ABMTheme
' to access the constants
Private ABM As ABMaterial 'ignore
' name of the page, must be the same as the class name (case sensitive!)
Public Name As String = "FirmwareUpdaterPage" '<-------------------------------------------------------- IMPORTANT
' will hold the unique browsers window id
Private ABMPageId As String = ""
' ' your own variables
' Private myToastId As Int
' the download folder
' Public DownloadFolder As String ="/www/" & ABMShared.AppName & "/uploads/"
Public DownloadFolder As String ="uploads"
Public DownloadMaxSize As String = 100*1024
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
' build the local structure IMPORTANT!
BuildPage
End Sub
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
Log("Connected")
ws = WebSocket1
ABMPageId = ABM.GetPageID(page, Name,ws)
Dim session As HttpSession = ABM.GetSession(ws, ABMShared.SessionMaxInactiveIntervalSeconds)
'----------------------START MODIFICATION 4.00-------------------------------
If session.IsNew Then
session.Invalidate
ABMShared.NavigateToPage(ws, "", "./")
Return
End If
'----------------------END MODIFICATION 4.00-------------------------------
If ABMShared.NeedsAuthorization Then
If session.GetAttribute2("IsAuthorized", "") = "" Then
ABMShared.NavigateToPage(ws, ABMPageId, "../")
Return
End If
End If
ABM.UpdateFromCache(Me, ABMShared.CachedPages, ABMPageId, ws)
If page.ComesFromPageCache Then
' when we have a page that is cached it doesn't matter if it comes or not from a new connection we serve the cached version.
Log("Comes from cache")
page.Refresh
page.FinishedLoading
Else
If page.WebsocketReconnected Then
Log("Websocket reconnected")
' when we have a client that doesn't have the page in cache and it's websocket reconnected and also it's session is new - basically when the client had internet problems and it's session (and also cache) expired before he reconnected so the user has content in the browser but we don't have any on the server. So we need to reload the page.
' when a client that doesn't have the page in cache and it's websocket reconnected but it's session is not new - when the client had internet problems and when he reconnected it's session was valid but he had no cache for this page we need to reload the page as the user browser has content, reconnected but we have no content in cache
ABMShared.NavigateToPage (ws, ABMPageId, "./" & page.PageHTMLName)
Else
' when the client did not reconnected it doesn't matter if the session was new or not because this is the websockets first connection so no dynamic content in the browser ... we are going to serve the dynamic content...
Log("Websocket first connection")
page.Prepare
ConnectPage
End If
End If
Log(ABMPageId)
' this page uses uploads, so needs some settings
ws.Session.SetAttribute("abmcallback", Me)
ws.Session.SetAttribute("abmdownloadfolder", DownloadFolder)
ws.Session.SetAttribute("abmmaxsize", DownloadMaxSize)
End Sub
Private Sub WebSocket_Disconnected
Log("Disconnected")
' and remove them again
Try
ws.Session.RemoveAttribute("abmcallback")
ws.Session.RemoveAttribute("abmdownloadfolder")
ws.Session.RemoveAttribute("abmmaxsize")
Catch
Log(LastException.Message)
End Try
End Sub
Sub Page_ParseEvent(Params As Map)
Dim eventName As String = Params.Get("eventname")
Dim eventParams() As String = Regex.Split(",",Params.Get("eventparams"))
If eventName = "beforeunload" Then
Log("preparing for url refresh")
ABM.RemoveMeFromCache(ABMShared.CachedPages, ABMPageId)
Return
End If
Dim caller As Object = page.GetEventHandler(Me, eventName)
If caller = Me Then
If SubExists(Me, eventName) Then
Params.Remove("eventname")
Params.Remove("eventparams")
' BEGIN NEW DRAGDROP
If eventName = "page_dropped" Then
page.ProcessDroppedEvent(Params)
End If
' END NEW DRAGDROP
Select Case Params.Size
Case 0
CallSub(Me, eventName)
Case 1
CallSub2(Me, eventName, Params.Get(eventParams(0)))
Case 2
If Params.get(eventParams(0)) = "abmistable" Then
Dim PassedTables As List = ABM.ProcessTablesFromTargetName(Params.get(eventParams(1)))
CallSub2(Me, eventName, PassedTables)
Else
CallSub3(Me, eventName, Params.Get(eventParams(0)), Params.Get(eventParams(1)))
End If
Case Else
' cannot be called directly, to many param
CallSub2(Me, eventName, Params)
End Select
End If
Else
CallSubDelayed2(caller, "ParseEvent", Params) 'ignore
End If
End Sub
public Sub BuildTheme()
' start with the base theme defined in ABMShared
theme.Initialize("pagetheme")
theme.AddABMTheme(ABMShared.MyTheme)
' add additional themes specific for this page
End Sub
public Sub BuildPage()
' initialize the theme
BuildTheme
' initialize this page using our theme
page.InitializeWithTheme(Name, "/ws/" & ABMShared.AppName & "/" & Name, False, ABMShared.SessionMaxInactiveIntervalSeconds, theme)
page.ShowLoader=True
page.PageHTMLName = "index.html"
page.PageTitle = "Update Firmware"
page.PageDescription = ""
page.PageKeywords = ""
page.PageSiteMapPriority = ""
page.PageSiteMapFrequency = ABM.SITEMAP_FREQ_YEARLY
' adding a navigation bar
ABMShared.BuildNavigationBar(page, "Update Firmware Menu","../images/logoGTC.png", "", "Setting", "UpdateFirmware")
' create the page grid
page.AddRows(3,True, "").AddCells12(1,"")
page.BuildGrid 'IMPORTANT once you loaded the complete grid AND before you start adding components
End Sub
public Sub ConnectPage()
ABMShared.ConnectNavigationBar(page)
' your stuff
' add header
page.Cell(1,1).AddComponent(ABMShared.BuildHeader(page, "hdr1", "Update Firmware "))
' add Paragraph
page.Cell(1,1).AddComponent(ABMShared.BuildParagraph(page, "par1", "Pilih File .jar untuk mengupdate firmware"))
Dim upload1 As ABMUpload
upload1.Initialize(page, "upload1", "Drop Here", "Browse", "upload")
page.Cell(2,1).AddComponent(upload1)
Dim inp1 As ABMFileInput
inp1.Initialize(page, "inp1", "Select a file", "Open", True, "input", "button")
page.Cell(3,1).AddComponent(inp1)
page.Refresh ' IMPORTANT
' NEW, because we use ShowLoaderType=ABM.LOADER_TYPE_MANUAL
page.FinishedLoading 'IMPORTANT
page.RestoreNavigationBarPosition
End Sub
'Sub Page_Dropped(Params As Map)
' If Params.Get("before") = "" Then
' Log("'" & Params.Get("component") & "' moved from " & Params.Get("source") & " to " & Params.Get("target"))
' Else
' Log("'" & Params.Get("component") & "' moved from " & Params.Get("source") & " to " & Params.Get("target") & " before component " & Params.Get("before"))
' End If
'End Sub
Sub inp1_Changed(value As String)
Log("value : " & value)
Dim inp1 As ABMFileInput = page.Component("inp1")
inp1.UploadToServer 'Here the file is uploading
End Sub
' clicked on the navigation bar
Sub Page_NavigationbarClicked(Action As String, Value As String)
' saving the navigation bar position
page.SaveNavigationBarPosition
If Action = "ABMUpload" Then Return
If Action = "LogOff" Then
ABMShared.LogOff(page)
Return
End If
ABMShared.NavigateToPage(ws, ABMPageId, Value)
End Sub
Sub Page_FileUploaded(FileName As String, success As Boolean)
If success Then
Log ("File is Uploaded")
Else
Log ("File is Failed to Uploaded")
End If
page.ws.Flush '// IMPORTANT
End Sub