B4J Question Handling Multiple File Upload?

tchart

Well-Known Member
Licensed User
Longtime User
I have an HTML form which has a multiple file upload element.

B4X:
<input type="file" name="files[]" id="files" multiple="" accept="image/*" onchange="fileinfo()">

The form handles the multiple files fine and I can see its picked up all the files selected.

However on the server side I only see the first file.

Has anyone been able to handle multi-file uploads?

Here is a code snippet;

B4X:
        If req.ContentType.StartsWith("multipart/form-data;") Then 'multipart/form-data; boundary=----WebKitFormBoundaryAJalm1Gxe1sTTGcy
            'parse the multipart data
            Dim parts As Map = req.GetMultipartData(File.DirApp & "/www/uploads/", 10000000)
           
            Log(parts.Size)
           
            Dim Encoding As String = req.CharacterEncoding
           
            Dim n As Int
           
            For n = 0 To parts.Size-1
                Log("Key = " & parts.GetKeyAt(n))
                Log("Value = " & parts.GetValueAt(n))
               
                Dim ThisPart As Part = parts.GetValueAt(n)               
                'Log(ThisPart.GetValue(Encoding))
               
                If parts.GetKeyAt(n) = "files[]" Then
                    Log(ThisPart.SubmittedFilename)
                End If
            Next
 

OliverA

Expert
Licensed User
Longtime User
I've had a single upload JS/B4J WebSocket implementation that I modded (because of this question) to handle multiple-file upload. In my case, I'm only dealing with a very simple form that has just the upload functionality (I'm not capturing any other input) and therefore this may not be the right direction for you (also I noticed your thread Async XMLHttpRequest & Multiple File Upload, which indicates you may have found your own solution). I've tried document the code with the links of the various sources I used for the implementation. I'm neither a B4J nor JS expert (really, just a beginner), so any input/critiques are welcome. In this post (https://www.b4x.com/android/forum/t...-use-setmaxtextmessagesize.45644/#post-281127), @Erel does mention that WebSockets may not be the best way for handling file uploads.

PS: In order to access the upload button, use the following URL after running the app
B4X:
http://localhost:51060/scanner2.html

Please ignore any weird naming conventions, since this code is ripped out of an ongoing project.
 

Attachments

  • MultiUploadJS.zip
    19 KB · Views: 348
Upvote 0

tchart

Well-Known Member
Licensed User
Longtime User
@OliverA thank you for that, I will have a look at the code. You are correct, I do have a solution now so I'm just ironing out some kinks but it is working nicely (apart from the server not recognizing some of the uploads as files)
 
Upvote 0
Top