B4J Question WebSocket with File Upload Example (with 2 upload fields)

Using the same example from the WebSocket with File Upload Example (which comes in the ServerExampleNoMySQL project), I would like to know how to modify the Handle of the FileHelper sub and the FileUploaded sub of the WebSocketwithFileUpload to handle two upload fields. The idea is to have two upload fields to upload both files at the same time using the same form?
index.html:
<input type="file" name="file1">
<input type="file" name="file2">

How to modify this code to accept two upload fields?
FileHelper:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    'get the callback module from the session (multiple modules can use this handler)
    Dim callback As Object = req.GetSession.GetAttribute("file_upload_sender")
    Try
        Dim data As Map = req.GetMultipartData(Main.TempDir, 100000)
        CallSubDelayed2(callback, "FileUploaded", data)
    Catch
        CallSubDelayed2(callback, "FileError", LastException.Message)
        resp.SendError(500, LastException.Message)
    End Try
End Sub


and How to modify this code to accept two upload fields? for example
WebSocketWithFileUpload:
Public Sub FileUploaded(parts As Map)
    Dim filePart As Part = parts.Get("file1")
    Result.SetText("File uploaded successfully: " & filePart.SubmittedFilename & _
        " size = " & NumberFormat(File.Size("", filePart.TempFile) / 1000, 0, 2) & "kb")

    Result.SetCSS("color", "black")
    File.Delete("", filePart.TempFile)
    ws.Flush 'this is a server event so we need to explicitly call Flush
End Sub

Thank you very much in advance for any guidance you can give me.
 

teddybear

Well-Known Member
Licensed User
How to modify this code to accept two upload fields?
FileHelper:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    'get the callback module from the session (multiple modules can use this handler)
    Dim callback As Object = req.GetSession.GetAttribute("file_upload_sender")
    Try
        Dim data As Map = req.GetMultipartData(Main.TempDir, 100000)
        CallSubDelayed2(callback, "FileUploaded", data)
    Catch
        CallSubDelayed2(callback, "FileError", LastException.Message)
        resp.SendError(500, LastException.Message)
    End Try
End Sub
You don't need to do anything.
and How to modify this code to accept two upload fields? for example
WebSocketWithFileUpload:
Public Sub FileUploaded(parts As Map)
    Dim filePart As Part = parts.Get("file1")
    Result.SetText("File uploaded successfully: " & filePart.SubmittedFilename & _
        " size = " & NumberFormat(File.Size("", filePart.TempFile) / 1000, 0, 2) & "kb")

    Result.SetCSS("color", "black")
    File.Delete("", filePart.TempFile)
    ws.Flush 'this is a server event so we need to explicitly call Flush
End Sub
You just add Dim filePart2 As Part = parts.Get("file2") to set result.settext for showing file2 info.
 
Upvote 0
Top