Android Question Multipart formdata problem

Sub7

Active Member
Licensed User
Longtime User
Whats wrong with my code?
I'm tring to send mixed data from device to my server, it fail while sending the image file.

B4X:
Sub btnsave_Click
    ProgressDialogShow("working")
    hc.Initialize("hc")
    'Add files
    Dim files As List
    files.Initialize
    Dim fd As FileData
    fd.Initialize
    fd.Dir = File.DirInternal
    fd.FileName = "profilepicture.jpeg"
    fd.KeyName = "profile_image" //
    fd.ContentType = "image/jpeg"
    files.Add(fd)
    'Add name / values pairs (parameters)
    Dim NV As Map
    NV.Initialize
    NV.Put("username", "xxx")
    NV.Put("password", "efdfe")
    NV.Put("country", "dfdfdf")
    NV.Put("age", "dfsddfdsf")
    Dim req As HttpRequest
    req = process_register.CreatePostRequest("http://www.website.net/app/register.php", NV, files)
    hc.Execute(req, 1)
End Sub

My php code:

B4X:
if (isset($_POST['username'], $_POST['password'],  $_POST['country'], $_POST['age']))
{
require("db.inc.php");
require("create_account.php");
require("encrypt.php");
$_POST['username'] = strtolower($_POST['username']);
$_POST['password'] = strtolower($_POST['password']);
$username = strip_tags(mysql_real_escape_string($_POST['username']));
$password = strip_tags(mysql_real_escape_string($_POST['password']));
$password = md5($username.$sha1.$password.$sha2.$username);
$country = strip_tags(mysql_real_escape_string($_POST['country']));
$age = $_POST['age'];
$registration_ip = $_SERVER['REMOTE_ADDR'];
//$profile_picture = $_FILES['profile_image'];

$profile_picture = file_get_contents('php://input', 'r');
$data = array();
parse_str($profile_picture, $data);
$profile_picture = $data['profile_image'];

$userRegistration = array("USERNAME" => $username,
                          "PASSWORD" => $password,
                          "COUNTRY" => $country,
                          "AGE" => $age,
                          "PROFILE_PICTURE" => $profile_picture,
                          "REGISTRATION_IP" => $registration_ip);

//print_r($userRegistration);


createAccount($userRegistration); //this function handles the user registration queryes and files

}

The problem is with image from Android, because when i try run this with a form i created on my server everything works fine.
I used this code: http://www.b4x.com/android/forum/threads/android-http-multipart-requests.8411/#content

thanks
 

Sub7

Active Member
Licensed User
Longtime User
Whats wrong with my code?
I'm tring to send mixed data from device to my server, it fail while sending the image file.

B4X:
Sub btnsave_Click
    ProgressDialogShow("working")
    hc.Initialize("hc")
    'Add files
    Dim files As List
    files.Initialize
    Dim fd As FileData
    fd.Initialize
    fd.Dir = File.DirInternal
    fd.FileName = "profilepicture.jpeg"
    fd.KeyName = "profile_image" //
    fd.ContentType = "image/jpeg"
    files.Add(fd)
    'Add name / values pairs (parameters)
    Dim NV As Map
    NV.Initialize
    NV.Put("username", "xxx")
    NV.Put("password", "efdfe")
    NV.Put("country", "dfdfdf")
    NV.Put("age", "dfsddfdsf")
    Dim req As HttpRequest
    req = process_register.CreatePostRequest("http://www.website.net/app/register.php", NV, files)
    hc.Execute(req, 1)
End Sub

My php code:

B4X:
if (isset($_POST['username'], $_POST['password'],  $_POST['country'], $_POST['age']))
{
require("db.inc.php");
require("create_account.php");
require("encrypt.php");
$_POST['username'] = strtolower($_POST['username']);
$_POST['password'] = strtolower($_POST['password']);
$username = strip_tags(mysql_real_escape_string($_POST['username']));
$password = strip_tags(mysql_real_escape_string($_POST['password']));
$password = md5($username.$sha1.$password.$sha2.$username);
$country = strip_tags(mysql_real_escape_string($_POST['country']));
$age = $_POST['age'];
$registration_ip = $_SERVER['REMOTE_ADDR'];
//$profile_picture = $_FILES['profile_image'];

$profile_picture = file_get_contents('php://input', 'r');
$data = array();
parse_str($profile_picture, $data);
$profile_picture = $data['profile_image'];

$userRegistration = array("USERNAME" => $username,
                          "PASSWORD" => $password,
                          "COUNTRY" => $country,
                          "AGE" => $age,
                          "PROFILE_PICTURE" => $profile_picture,
                          "REGISTRATION_IP" => $registration_ip);

//print_r($userRegistration);


createAccount($userRegistration); //this function handles the user registration queryes and files

}

The problem is with image from Android, because when i try run this with a form i created on my server everything works fine.
I used this code: http://www.b4x.com/android/forum/threads/android-http-multipart-requests.8411/#content

thanks

I found in forum that the problem was here, now it works.

b = ("--" & boundary & EOL & "Content-Disposition: form-data; name=" _
& QUOTE & FD.KeyName & QUOTE & "; filename=" & QUOTE & FD.FileName & QUOTE _
& EOL & "Content-Type: " & FD.ContentType & EOL & EOL).GetBytes("UTF8")
 
Upvote 0
Top