B4J Question server receive post file, the file added prefix and suffix?

bigluo1

New Member
i have a b4j server, receive file from client, the client is other program, i use postman to test.
i send a text file(test.txt), the content is:
B4X:
BEGIN
line1
line2
line3
END
my server receive the post file, save to disk, the content is:
B4X:
----------------------------617993333513995102880868
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain

BEGIN
line1
line2
line3
END
----------------------------617993333513995102880868--
my server's code is:
B4X:
If req.Method <> "POST" Then
	'error msg......
	Return
End If
Dim In As InputStream = req.InputStream
Dim filename As String = "filename"
Dim filepath As String = "filepath"
Dim out As OutputStream = File.OpenOutput(filepath, filename, False)
File.Copy2(In, out)
out.Close
what's wrong with my code?
i test png/jpg files too, the question still exist.
thanks.
 

bigluo1

New Member
maybe i found the solution, test with single file, text file, png file, result is correct:
B4X:
If req.ContentType.Contains("form-data") Then
	Dim m As Map = req.GetMultipartData(File.DirApp & "/tempUpload", 2048000)
	For i = 0 To m.Size - 1
		Dim part As Part = m.Get(m.GetKeyAt(i))
		If part.TempFile <> "" Then
			Dim fTmpName As String = part.TempFile
			Dim fNewName As String = part.SubmittedFilename
			'Log(fTmpName)
			'Log(fNewName)                                                            
			File.Copy("", fTmpName, File.DirApp, fNewName)
			File.Delete("", fTmpName)
			Exit  'i only need one file
		Else
			Log("not a file")
		End If
	Next
End If
 
Upvote 0
Top