How to post float, int, string and PNG in the same form ?

Hi,

I’d like to know how (if it’s possible) to post float, int, string variables and PNG (screenshots took from cameras, and using .EncodeToPNG() function) within the same WWWForm ?

For now, it’s only working if i post all the floats, ints, strings in one form and the pictures in another.

Thanks. :slight_smile:

When i did my screenshot app, i had to send the picture seperatly

WWWForm.AddBinaryData should do the trick

example from the Script Reference

// Grab a screen shot and upload it to a CGI script.
// The CGI script must be able to hande form uploads.
var screenShotURL= "http://www.my-server.com/cgi-bin/screenshot.pl";
// Take a screen shot immediately
function Start() {
    UploadPNG();
}
function UploadPNG() {
    // We should only read the screen after all rendering is complete
    yield WaitForEndOfFrame();
    // Create a texture the size of the screen, RGB24 format
    var width = Screen.width;
    var height = Screen.height;
    var tex = new Texture2D( width, height, TextureFormat.RGB24, false );
    // Read screen contents into the texture
    tex.ReadPixels( Rect(0, 0, width, height), 0, 0 );
    tex.Apply();
    // Encode texture into PNG
    var bytes = tex.EncodeToPNG();
    Destroy( tex );
    // Create a Web Form
    var form = new WWWForm();
    form.AddField("frameCount", Time.frameCount.ToString());
    form.AddBinaryData("fileUpload", bytes, "screenShot.png", "image/png");
    // Upload to a cgi script
    var w = WWW(screenShotURL, form);
    yield w;
    if (!String.IsNullOrEmpty(w.error))
        print(w.error);
    else
        print("Finished Uploading Screenshot");
}
2 Likes

Here are the functions I’m using to post datas :

public IEnumerator OnBtnValidateEvent()
    {
        // Form creation
        WWWForm form = new WWWForm();
       
        // Filling the form
        form.AddField("ID Pet", petID);
        if (breedID != 0) form.AddField("ID Breed", breedID);
        else form.AddField("ID Morphotype", morphID);
        form.AddField("Score BCS", score);
        form.AddField("Value Loin", sldLoinValue.ToString());
        form.AddField("Value Abdomen area", sldAreaValue.ToString());
        form.AddField("Value Hip", sldHipValue.ToString());
        form.AddField("Value Thigh", sldThighValue.ToString());
        form.AddField("Value Rib", sldRibValue.ToString());
        
        // Taking screenshots
        StartCoroutine("takePics");

        // Uploading the form
        WWW upload = new WWW(url, form);
        yield return upload;
        if (upload.error != null)
            Debug.Log("ERROR while uploading form : " + upload.error);
        else
            Debug.Log("Form successfully uploaded");
    }

    // Prise screenshots
    public IEnumerator takePics()
    {
        // Reset position
        this.GetComponent<LoadSceneBundle>().ResetPosition();

        foreach (GameObject go in GameObject.FindGameObjectsWithTag("picCam"))
        {
            yield return new WaitForEndOfFrame();

            Debug.Log(go.name);
            Camera cam = go.GetComponent<Camera>();
            resWidth = (int)cam.pixelWidth;
            resHeight = (int)cam.pixelHeight;
            cam.Render();

            Texture2D texture = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);

            texture.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
            texture.Apply();

            yield return 0;

            bytes = texture.EncodeToPNG();

            name++;

            form.AddField("action", "Upload Image");
            form.AddBinaryData("fileUpload", bytes, "img_" + name.ToString() + ".png", "image/png");

            DestroyObject(texture);
        }
    }
<?php
if ( isset ($_POST['action']) ) {
if($_POST['action'] == "Upload Image") {
unset($imagename);

if(!isset($_FILES)  isset($HTTP_POST_FILES)) $_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['fileUpload'])) $error["image_file"] = "An image was not found.";

$imagename = basename($_FILES['fileUpload']['name']);

if(empty($imagename)) $error["imagename"] = "The name of the image was not found.";

if(empty($error)) {
$newimage = "images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['fileUpload']['tmp_name'], $newimage);
if ( empty($result) ) $error["result"] = "There was an error moving the uploaded file.";
}
}
} else {
echo "no form data found";
}
?>
2 Likes