Android Question Upload file with php script

Addo

Well-Known Member
Licensed User
I have a PHP script with a specific useragent and isset POST Checking Feild
PHP:
$ua = $_SERVER["HTTP_USER_AGENT"];  

if(strlen(strstr($ua,"AbrTus")) <= 0 ){ 
Die('Invalid U Agent');
}
if (isset($_POST['adelreq'])) {

$imgsize = $_FILES['image']['size'];
$image_name = $_FILES['image']['name'];
$image_tempname = $_FILES['image']['tmp_name'];
$image_type = getimagesize($image_tempname);
$image_ext = explode('.',$image_name);
$ext = strtolower(end($image_ext));
$FileError = $_FILES['image']['error'];
$allow_ext = array('png','jpg','gif','jpeg','bmp');
$allow_type = array('image/png','image/gif','image/jpeg','image/bmp');
//..........


I tried the following in order to upload an image through that script

B4X:
Sub uploadimage(imgtoup As String)
 Dim j As HttpJob
 Dim img As String = imgtoup
 
 
Log("startup")
 
j.Initialize("", Me)
Dim mp As MultipartFileData
mp.Initialize

mp.Dir = File.DirAssets

mp.FileName = img

mp.KeyName = "image"

j.PostMultipart(SuploadScript, Null, Array(mp))

j.GetRequest.SetHeader("User-Agent", "AbrTus")

Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log("data: "&j.GetString)
End If

j.Release
End Sub

but I couldn't succeed to upload which probably I did not include adelreq Field to the posted data

I used to upload in Delphi through this code


B4X:
PostData := TMultipartFormData.Create;
HttpClient.UserAgent := 'AbrTus';
postdata.AddField('adelreq','');
PostData.AddFile('image', FFileToUpload);
HttpResponse := HttpClient.Post(FdataURL, PostData);
FoutData := HttpResponse.ContentAsString(TEncoding.UTF8);

but I couldn't figure out how to do the same approach in b4a
 

Addo

Well-Known Member
Licensed User
Solved. Final code

B4X:
Sub uploadimage(imgtoup As String)
 Dim j As HttpJob
 Dim img As String = imgtoup
 
 
j.Initialize("", Me)
Dim mp As MultipartFileData
mp.Initialize

mp.Dir = File.DirAssets

mp.FileName = img
mp.KeyName = "image"

j.PostMultipart(SuploadScript, CreateMap("adelreq":""), Array(mp))

j.GetRequest.SetHeader("User-Agent", sUserAgent)

Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log("data: "&j.GetString)
End If

j.Release
End Sub
 
Upvote 0
Top