- Home /
How to upload a local file via the web player?
I have a problem using the Iframe while I try to upload a file. The problem occurred in the ShowFile JavaScript function. I am able to call alert(blabla); but nothing else works. So I can upload the file but can't send a message to unity to say what it is.
Here's what I got for now:
An IFrame in my html page.
<iframe id="myFrame"></iframe>
two functions in an external javascript
function UploadFile()
{
var $frame = $('#myFrame');
$frame.attr('src','../inc/upload.html');
$frame.css('top',($('#unityPlayer').innerWidth()/2)-($frame.width()/2)-50);
$frame.css('left',($(window).innerWidth()/2)-($frame.width()/2));
$frame.show();
}
function ShowFile(path)
{
alert(blabla); <---- this works but anything that follow doesn't
$('#myFrame').hide();
u.getUnity().SendMessage("Uploader", "ShowImage", path);
}
upload.html looks like this
<!DOCTYPE html>
<html>
<body>
<form action="fileUpload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
and it calls fileUpload.php which looks like this
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "
";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "FileUploaded/" . $_FILES["file"]["name"]);
$path = "inc/FileUploaded/" . $_FILES["file"]["name"];
echo '<script>ShowFile("'.$path.'");</script>';
}
?>
Finally I have an object in unity named Uploader with a script that contains this code:
public class uploader : MonoBehaviour {
bool showImage = false;
string path = "";
void OnGUI()
{
if(GUI.Button(new Rect(100,300,100,40), "Upload"))
{
Application.ExternalCall("UploadFile");
}
if(showImage)
{
GUI.Label(new Rect(10,400,200,30), "blablabla");
}
}
public void ShowImage(string path)
{
showImage = true;
this.path = path;
}
}
Everything is on a local server and if I call u.getUnity().SendMessage("Uploader", "ShowImage", path); from uploadFile, it is actually called.
If anyone got an idea why the lines are not called, please let me know. Thanx!
As per my understanding, the web player has zero access to the hard drive. I think what you're trying to do is impossible. I may be wrong.
Answer by Seizure · Aug 01, 2013 at 08:49 PM
It is very possible, the webplayer will talk with your server that can hold on to your files for you, this is very useful to keep file size down and only load files when needed. As to what is wrong I do not particularly see anything, I use C# and I always use coroutines and use a yield return w; (basically wait until the form has come back competely before continuing)
The problem is that I can't reach The IFrame neither the web player after the operation with the php file, but it is strange that I can reach the javascript which is in the same page in reality(via external javascript).
I can't seem to find a way using the coroutine and yield stuff for a file upload.
Thx for your answer.
Your answer
Follow this Question
Related Questions
Webplayer Log output missing stacktrace. 1 Answer
A node in a childnode? 1 Answer
How to Browse a photo or Open to gallery 3 Answers
Loading External Files for Web Deployment 1 Answer
Creating a save.xml file and a folder to stash it in 1 Answer