- Home /
uploading screenshot to server
Hi guys
I am trying to upload a screenshot of my scene at the server using the WWW class. You can find here http://www.ionio.gr/~av200420/screen/screen.html my simple application. While clicking the cube an alert message says that the screenshot uploaded. I read that i should have a crossdomain.xml file, so, i uploaded here http://www.ionio.gr/~av200420/crossdomain.xml. In unity now I assign the screenshot url var screenShotURL= "http://www.ionio.gr/~av200420"; My problem is that unity says that everything is OK but i can find somewhere the screenshot to my server... Is there any problem with the assigned values or routs?
Thanks!
Hi Guys,
You can use this plugin Easy Downloader and Uploader
so, did you guys made it work? i tried using the sample codes here and it did not work for me, i keep getting the "Finished Uploading Screenshot" but there is no screenshot on my server
I used the same cod and i get the upload complet message, but there is no img on my server, can anyone help me?
Answer by Bunny83 · Jun 18, 2011 at 03:23 AM
Your upload url doesn't look like there's anything on your server that can receive the upload? Do you have a serverside php script that saves the upload? It seems you forget that...
To take a screenshot and upload it there's an example in the Unity docs.
Here's a similar question where you can see how the serverside script can look like:
http://answers.unity3d.com/questions/48686/uploading-photo-and-video-on-a-web-server.html
Answer by miketucker · Jan 10, 2012 at 03:14 PM
Unityscript:
var screenShotURL= "http://localhost:8888/upload_file.php";
function Start(){
//print(Application.srcPath);
}
function OnMouseDown() {
// 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("file", bytes, "screenShot.png", "image/png");
// Upload to a cgi script
var w = WWW(screenShotURL, form);
yield w;
if (w.error != null){
print(w.error);
Application.ExternalCall( "debug", w.error);
//print(screenShotURL);
}
else{
print("Finished Uploading Screenshot");
//print(screenShotURL);
Application.ExternalCall( "debug", "Finished Uploading Screenshot");
}
}
PHP:
<?php
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000000000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "
"; } else { echo "Upload: " . $_FILES["file"]["name"] . "
"; echo "Type: " . $_FILES["file"]["type"] . "
"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
} else { echo "Invalid file"; }
?>
I spotted a little error here. You're uploading a .png file, but in php code the first condition is only for .gif, .jpeg and .pjpeg. you need to add ($_FILES["file"]["type"] == "image/png") to the first if statement.
Answer by chmo · Jun 19, 2011 at 01:33 AM
I don't know why but it doesn't work yet. I am using this javascript
var screenShotURL= "http://localhost:8888/upload_file.php";
//var screenShotURL= "http://195.130.124.68/cgi-bin/screenshot.pl";
function Start(){ //print(Application.srcPath); }
function OnMouseDown() { // 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 (w.error != null){
print(w.error);
Application.ExternalCall( "debug", w.error);
//print(screenShotURL);
}
else{
print("Finished Uploading Screenshot");
//print(screenShotURL);
Application.ExternalCall( "debug", "Finished Uploading Screenshot");
}
}
and this php script
0) { echo "Return Code: " . $_FILES["file"]["error"] . "
"; } else { echo "Upload: " . $_FILES["file"]["name"] . "
"; echo "Type: " . $_FILES["file"]["type"] . "
"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
} else { echo "Invalid file"; } ?>
The program sais that the image uploaded... but there is no image inside the upload folder. Is there any wrong with the scripts? Do I want any other script in order to upload the screenshot???
Thanks again!
Hi there did you work this out in the end? We are looking at trying to upload pictures into a web player app built in Unity too.
the above is correct but simply fix the name of the file being uploaded. Currently you have:
form.AddBinaryData("fileUpload", bytes, "screenShot.png", "image/png");
change 'fileUpload' to 'file'