iOS Question Upload file

nwhitfield

Active Member
Licensed User
Longtime User
This is what I've got - pretty much unchanged from the B4a code, except that it's called in the Complete event for the camera, to allow them to upload a photo from the gallery

The form parameters USERKEY and APPKEY are used to help authenticate the user, and the boundary could be any old string. On the PHP side, those are verified against the database, and then if all went well, the function is_uploaded_file($_FILES['BLUFupload']['tmp_name']) returns True, and you just process the image as usual. In this case, the name specified in the form is pretty much irrelevant

B4X:
Sub uploadButton_Click
cam.Initialize("cam",profilePage)
cam.SelectFromSavedPhotos(profilePage.RootPanel,cam.TYPE_IMAGE)
End Sub

Sub cam_Complete (Success As Boolean, Image As Bitmap, VideoPath As String)
If Success Then
If Image.IsInitialized Then
hud.ProgressDialogShow("Uploading photo to BLUF")
Dim boundary As String = "BMU" & BLUF.authKey
Dim EOL As String = Chr(13) & Chr(10)

Log("Boundary is " & boundary)
Log("App key is " & BLUF.loginKey)

Dim form As StringBuilder
form.Initialize
form.Append(EOL).Append("--").Append(boundary).Append(EOL)

form.Append("Content-Disposition: form-data; name=""USERKEY""").Append(EOL).Append(EOL)
form.Append(BLUFsettings.Get("authcode"))
form.Append(EOL).Append("--").Append(boundary).Append(EOL)
form.Append("Content-Disposition: form-data; name=""APPKEY""").Append(EOL).Append(EOL)
form.Append(BLUF.loginKey)
form.Append(EOL).Append("--").Append(boundary).Append(EOL)
form.Append("Content-Disposition: form-data; name=""BLUFupload""; filename=""BLUFuploadTMP.jpg""").Append(EOL)
form.Append("Content-Type: image/jpeg").Append(EOL)
form.Append("Content-Transfer-Encoding: binary").Append(EOL).Append(EOL)

Dim formData() As Byte
formData = form.ToString.GetBytes("UTF8")

Dim formStream As OutputStream
formStream.InitializeToBytesArray(formData.Length)

formStream.WriteBytes(formData,0,formData.Length)
' now formStream has the beginning, add the actual file

Image.WriteToStream(formStream,90,"JPEG")

' put the final boundary on the end of the form

Dim tail As StringBuilder
tail.Initialize
tail.Append(EOL).Append("--").Append(boundary).Append(EOL)
formStream.WriteBytes(tail.ToString.GetBytes("UTF8"),0,tail.Length)

' post to the URL

Log("Form prepared")

Dim uploader As HttpJob
uploader.Initialize("upload",BLUFios)
uploader.PostBytes(BLUF.uploadURL,formStream.ToBytesArray)
uploader.GetRequest.SetContentType("multipart/form-data; boundary=" & boundary)
uploader.GetRequest.SetHeader("User-Agent",BLUF.blufVersion)

End If
End If
End Sub
 
Last edited:
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
This is what I've got - pretty much unchanged from the B4a code, except that it's called in the Complete event for the camera, to allow them to upload a photo from the gallery

The form parameters USERKEY and APPKEY are used to help authenticate the user, and the boundary could be any old string. On the PHP side, those are verified against the database, and then if all went well, the function is_upload_file($_FILES['BLUFupload']['tmp_name']) returns True, and you just process the image as usual. In this case, the name specified in the form is pretty much irrelevant

B4X:
Sub uploadButton_Click
cam.Initialize("cam",profilePage)
cam.SelectFromSavedPhotos(profilePage.RootPanel,cam.TYPE_IMAGE)
End Sub

Sub cam_Complete (Success As Boolean, Image As Bitmap, VideoPath As String)
If Success Then
If Image.IsInitialized Then
hud.ProgressDialogShow("Uploading photo to BLUF")
Dim boundary As String = "BMU" & BLUF.authKey
Dim EOL As String = Chr(13) & Chr(10)

Log("Boundary is " & boundary)
Log("App key is " & BLUF.loginKey)

Dim form As StringBuilder
form.Initialize
form.Append(EOL).Append("--").Append(boundary).Append(EOL)

form.Append("Content-Disposition: form-data; name=""USERKEY""").Append(EOL).Append(EOL)
form.Append(BLUFsettings.Get("authcode"))
form.Append(EOL).Append("--").Append(boundary).Append(EOL)
form.Append("Content-Disposition: form-data; name=""APPKEY""").Append(EOL).Append(EOL)
form.Append(BLUF.loginKey)
form.Append(EOL).Append("--").Append(boundary).Append(EOL)
form.Append("Content-Disposition: form-data; name=""BLUFupload""; filename=""BLUFuploadTMP.jpg""").Append(EOL)
form.Append("Content-Type: image/jpeg").Append(EOL)
form.Append("Content-Transfer-Encoding: binary").Append(EOL).Append(EOL)

Dim formData() As Byte
formData = form.ToString.GetBytes("UTF8")

Dim formStream As OutputStream
formStream.InitializeToBytesArray(formData.Length)

formStream.WriteBytes(formData,0,formData.Length)
' now formStream has the beginning, add the actual file

Image.WriteToStream(formStream,90,"JPEG")

' put the final boundary on the end of the form

Dim tail As StringBuilder
tail.Initialize
tail.Append(EOL).Append("--").Append(boundary).Append(EOL)
formStream.WriteBytes(tail.ToString.GetBytes("UTF8"),0,tail.Length)

' post to the URL

Log("Form prepared")

Dim uploader As HttpJob
uploader.Initialize("upload",BLUFios)
uploader.PostBytes(BLUF.uploadURL,formStream.ToBytesArray)
uploader.GetRequest.SetContentType("multipart/form-data; boundary=" & boundary)
uploader.GetRequest.SetHeader("User-Agent",BLUF.blufVersion)

End If
End If
End Sub
Very very useful, will try Many many thanks!
 
Upvote 0
Top