WWW class downloading image doesn't work in Web Player?

Hi all,

I’m fiddling with downloading images from a folder on my server to an array in Unity. It works in the editor and in .exe / .app builds, but not in the web build version. Any ideas? I’ll post relevant code below. I suspect it has something to do with improper coroutining, since I know nothing (basically) about coroutines.

var imageURLString:String="";
var isLoading:boolean=false;
private var textureArray = new Array();


function Start(){
	GetImageList("myurl.php");
	}


function GetImageList(url:String){
       //returns a string of images separated by semi-colons like this:
       //username,imageurl; username,imageurl; etc. etc.
	isLoading = true;
	textureArray = [];
	Debug.Log("getting it");
	var www : WWW = new WWW (url);
	yield www;
	if(www.error){Debug.Log("Error is " + www.error);
		returnString = www.error;
		}
	else {
		imageURLString = www.text;
		//returnString = www.text;
		}
        //now I send that string with ; and , separations to "Download Images"
	DownloadImages(imageURLString);
	}

function DownloadImages(str:String){
	if(str.Length<=1) return;
	//break the string up into [user+imgurl, user+imgurl, ... ]
	var imageStrings:String[] = str.Split(","[0]);
	for(var s in imageStrings){
		//break string up more, into [[user, imgurl], [user, imgurl], ...]
		var currArr : String[] = s.Split(";"[0]);
		var user = currArr[0];
		var url = currArr[1];
		// for each image, grab the image from the imgurl and pass the username too.
		GetImageFromURL(url, user);
		}
	isLoading = false;
	}


function GetImageFromURL (url:String, usr:String) {
	// Start a download of the given URL
	Debug.Log(url);
	var www : WWW = new WWW (url);
	// Wait for download to complete
	
		while(!www.isDone){
		yield;
		}
	//yield www;
	// assign texture;
	returnString = www.error;
	if(www.error) return;
	try{
		var t:Texture;
		try{
			t = www.textureNonReadable;
			}
		catch(err){
			Debug.Log(err);
			returnString=err.ToString();
			}		
		//FINALLY: push the username and the texture into an array.
		textureArray.push([usr, t]);
		}
	catch(err) {
		returnString = err.ToString();
		}
	
	}

It’s pretty much been comically frustrating. I’ve been messing around with coroutining different areas but it doesn’t seem to give. Any ideas?

EDIT: I’ll note that the error I get during this process I pass to a GUI element, and it says “Failed downloading url.com/images/image.jpeg

Hey everyone,

I went forward with my solution I mentioned in the comments section of my question. The following fixed my issue:

STEP 1: PHP Back-end

So I needed to, instead of reading files directly online, set up PHP to report the byte array of each image.

<?php
header('Content-type: image/jpeg');
if ($_POST) 
{
    if ( isset ($_POST['action']) ) 
    {
        if($_POST['action'] === 'gallery download') 
        { 
            $file = $_POST['file'];
            $path = 'images/uploads/' . $file;
            $type = pathinfo($path, PATHINFO_EXTENSION);

            $img = fopen($path, 'rb');
            $imgEncode = file_get_contents($path, FILE_USE_INCLUDE_PATH);
            echo $imgEncode;
        }
    }
}
?>

STEP 2: Modifying the javascript Unity-side

Super minimal changes are necessary :slight_smile: All changes needed were made to GetImageFromURL

function GetImageFromURL (file:String, usr:String) {
	 // Start a download of the given URL
	Debug.Log(file);
	var url:String = "myPHPurl.com/GetImageBytes.php";
	Debug.Log(url);
	
	//CHANGE 1: PASS DESIRED IMAGE URL TO A FORM
	var form = new WWWForm();
	form.AddField("action", "gallery download");
	form.AddField("file", file);
	
	var www : WWW = new WWW (url, form);
	// Wait for download to complete
	yield www;
	
	// assign texture;
	try{
		//CHANGE 2: READ THE TEXTURE FORMATTED BYTE ARRAY RETURNED BY THE FORM
		var byteTexture = www.texture;
		textureArray.push([usr, byteTexture]);
		}
	catch(err) {
		}
	}