I have the following code in B4A to upload a single picture taken with the smartphone camera to the server (localhost IIS server on my PC):
And on the server, in ASP:
The problem is that, although an 18 kb file, correctly named "test.jpg", appears in the root folder of my server correctly, the file appears corrupted. For example, if I open it with MSPaint or Windows Viewer, both programs say the file is corrupted (and the preview in File Manager doesn't show the preview of the image). Any ideas as to what might be wrong?
P.S., Is there an easier way to upload an image file (potentially large file) from the phone to the server, without introducing additional setup/components like another server, B4J, etc.? My thinking is that sending bytes of data should work...
B4X:
Dim fd As FileData
fd.Initialize
fd.Dir = Dir
fd.FileName = OrigFileName
fd.KeyName = "files"
fd.ContentType = "application/octet-stream"
files.Add(fd)
Dim NV AsMap
NV.Initialize
NV.Put("note1", "abc")
Dim req As OkHttpRequest
req = MultipartPost.CreatePostRequest("http://" & Starter.IPAddr & "/imgupload.aspx", NV, files)
hc.Execute(req, 1)
B4X:
Dim length As Integer = Convert.ToInt32(Context.Request.InputStream.Length)
Dim buffer() As Byte = New Byte((length) - 1) {}
Context.Request.InputStream.Read(buffer, 0, length)
Dim ms As MemoryStream = New MemoryStream(buffer)
Dim fileAs New FileStream(Server.MapPath("test.jpg"), FileMode.Create, FileAccess.Write)
ms.WriteTo(file)
file.Close()
ms.Close
P.S., Is there an easier way to upload an image file (potentially large file) from the phone to the server, without introducing additional setup/components like another server, B4J, etc.? My thinking is that sending bytes of data should work...
Last edited: