B4J Question Multipart Form-Data

masmukti

Member
Licensed User
Longtime User
Hi...
I am trying to send file using Post Form-Data to b4j Server,
I am using postman to try it,

if the file larger than about 85KB, its work,
but under 85KB its not work,

I am using java 8 and B4J 8,

any ideas?

Thanks
 

masmukti

Member
Licensed User
Longtime User
This is my code,
B4X:
Public Sub Handle(req As ServletRequest, resp As ServletResponse)
Dim m                     As Map
Dim fPart                 As Part
Dim fTmpName             As String
Dim fNewName             As String
Dim sf                    As JStringFunctions
Dim Keys(20)            As String
    
    
     sf.Initialize
    If     req.Method="POST" Then
        If     req.ContentType.Contains("multipart/form-data") Then
               Try
                  m         = req.GetMultipartData(File.DirApp, 100000000)         
                    
                 For xCount=0 To m.Size-1
                    Keys(xCount)     = m.GetKeyAt(xCount)           
                    fPart            = m.Get(Keys(xCount))
            
                     If     fPart.IsInitialized Then           
                        If     fPart.IsFile Then                       
                            fTmpName        = fPart.TempFile.SubString(sf.InString(fPart.TempFile, "MultiPart"))
                            fNewName        = fPart.SubmittedFilename
                                                                      
                             File.Copy(File.DirApp,         fTmpName, File.DirApp, fNewName)
                            File.Delete(File.DirApp,     fTmpName)
                                
                        Else
                            Log("Not File")
                            
                        End If
                    End If
                Next
            
               Catch
                Log(" Error: "&LastException.Message)
            
            End Try     
            
        End If
    End If

End Sub

I am trying with 86kb image and 41kb,
and just works with 86kb

thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The example is included in the "server example": https://www.b4x.com/android/forum/threads/webapp-web-apps-overview.39811/

It is a bit more complicated as it demonstrates file uploading with WebSocket handler.
The relevant code just calls:
B4X:
        Dim data As Map = req.GetMultipartData(Main.TempDir, 100000)
       'in other module
Dim filePart As Part = data.Get("file1")
    Result.SetText("File uploaded successfully: " & filePart.SubmittedFilename & _
        " size = " & NumberFormat(File.Size("", filePart.TempFile) / 1000, 0, 2) & "kb")

The truth is that code that uses Map.GetKeyAt and StringFunctions is code that should be fixed. Even if it works properly.
 
Upvote 0

masmukti

Member
Licensed User
Longtime User
I was tried few times and still not work
I attach the file that i tried,

Could you test it ?

Thank
 

Attachments

  • MultiPart_Test.zip
    27.3 KB · Views: 219
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0
Top