- Home /
The question is answered, right answer was accepted
How to upload an image from our system or device
How to upload an image using c# . In my game, need to upload Avatar picture . How to upload an image from our system or device.
Nick4 , i asking about uploading images, not about downloading
Anyone else know how to upload an image from our system or device
Nick4 is right.. you can use that concept to technically 'download' from your own hard drive. ill post an example in a sec
Answer by LijuDeveloper · Nov 20, 2014 at 05:10 AM
i've got the solution. I got a free plugin for file browser . C# File Browser in Asset Store.
1 .For File browser code please refer Accessing local system ( File Browser ) in Unity answers.
2.Code for uploading image
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Linq;
using System.IO;
using System ;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net;
public class c_BrowserManager : MonoBehaviour {
public GameObject fileBrowserObj;
public GameObject filePhoto;
public Texture defaultTexture;
private Texture fileTexture;
public tk2dTextMesh fileNameText;
private string imgUploadUrl = "http://google.com/game/data/image_upload?";// here your url
// Use this for initialization
void Start () {
c_global.fileFullName="";
fileBrowserObj.SetActive (false);
fileNameText.text ="Choose an image to upload";
}
// Update is called once per frame
void Update ()
{
if(c_global.isFileBrowserShow == true)
{
fileBrowserObj.SetActive (true);
c_global.isFileBrowserShow = false;
}
if(c_global.isFileBrowserHide == true)
{
fileBrowserObj.SetActive (false);
c_global.isFileCheck = true;
c_global.isFileBrowserHide = false;
}
if(c_global.isFileCheck == true)
{
if(c_global.fileFullName !="")
{
//print ("LLLLL");
fileNameText.text = c_global.fileFullName;
StartCoroutine (WaitForImageDisplay());
}
c_global.isFileCheck = false;
}
if(c_global.isFileUpdateOn == true)
{
if(c_global.fileFullName !="")
{
//print ("LLLLL");
fileNameText.text = c_global.fileFullName;
StartCoroutine (WaitForImageUpload());
}
else
{
fileNameText.text ="Please select an image";
}
c_global.isFileUpdateOn = false;
}
}
void PostImageInfo()
{
}
IEnumerator WaitForImageDisplay()
{
WWW localFile = new WWW ("file://"+c_global.fileFullName);
yield return localFile;
//print ("Image Display Calling");
if(localFile.error == null)
{
fileTexture = localFile.texture;
filePhoto.renderer.material.mainTexture = fileTexture;
}
else
{
filePhoto.renderer.material.mainTexture = defaultTexture;
//print ("Error :"+localFile.error);
}
}
IEnumerator WaitForImageUpload()
{
WWW localFile = new WWW ("file://"+c_global.fileFullName);
yield return localFile;
//print ("Image Display Calling");
if(localFile.error == null)
{
fileTexture = localFile.texture;
filePhoto.renderer.material.mainTexture = fileTexture;
}
else
{
filePhoto.renderer.material.mainTexture = defaultTexture;
//print ("Error :"+localFile.error);
yield break;
}
WWWForm myForm = new WWWForm();
// string result = System.Text.Encoding.UTF8.GetString(localFile.bytes);
// string result = System.Text.Encoding.UTF8.GetString(localFile.bytes);
FileInfo file = new FileInfo(c_global.fileFullName);
string fileExtension = file.Extension;
string []tempArray = fileExtension.Split("."[0]);
// int leng = fileExtension.Length ;
// char [] temp = fileExtension.ToCharArray ();
// char [] temp2 = new char[leng-1];
//
// for(int i=0,j=0;i<leng ;i++)
// {
// if(i>0)
// {
// temp2[j] = temp[i ] ;
// //print ("temp2["+j+"] = " +temp2[j] );
// j++;
// }
// }
// file
fileExtension = tempArray[1];
string result =Convert.ToBase64String(localFile.bytes);
//print ("Filetype :"+fileExtension );
//myForm.AddBinaryData ("img",localFile.bytes);
//myForm.AddField ("img",localFile.ToString());
myForm.AddField ("id",c_global.userId);
myForm.AddField ("type",fileExtension);
myForm.AddField ("img",result);
//print (" Image to string :"+result);
c_global.isImgUploadWinHide = true;
WWW upload = new WWW (imgUploadUrl ,myForm);
StartCoroutine (WaitForImageUploadToServer(upload));
/*
WWW upload = new WWW (imgUploadUrl ,myForm);
yield return upload;
if(upload.error ==null)
{
if( upload.text != "" )
{
XmlDocument l_doc = new XmlDocument ();
l_doc.LoadXml(upload.text);
XmlNode l_status = l_doc.SelectSingleNode("data/status");
//print(" Upload Status !"+l_status.InnerText);
}
else
{
//print(" Upload Error Null Text");
}
}
else
{
//print(" Upload Error :"+upload.error);
}*/
}
IEnumerator WaitForImageUploadToServer(WWW upload)
{
//WWW upload = new WWW (imgUploadUrl ,myForm);
yield return upload;
if(upload.error ==null)
{
if( upload.text != "" )
{
XmlDocument l_doc = new XmlDocument ();
c_global.isFileUpdateCompleted = true;
c_global.isImageUploadError = true;
c_global.isImageUploadErrorMsg = "Image uploaded successfully !";
// try
// {
//
//
// l_doc.LoadXml(upload.text);
//
// XmlNode l_status = l_doc.SelectSingleNode("data/status");
// //print(" Upload Status !"+l_status.InnerText);
// }
// catch (Exception e)
// {
// //print (" Exception in uploading :"+e);
// }
}
else
{
//print(" Upload Error Null Text");
}
}
else
{
//print(" Upload Error :"+upload.error);
c_global.isImageUploadError = true;
c_global.isImageUploadErrorMsg = "Image uploading fail !";
}
}
}
hello, what are the steps which you follow to do the upload?
hey thank for your great help but i have simple question what is c_global?
Answer by thornekey · Mar 11, 2014 at 10:44 AM
public Texture texNotFound;
private string filelocationinput = "";
void OnGUI ()
{
filelocationinput = GUI.TextField (new Rect (10, 10, 128, 20), filelocationinput);
if (GUI.Button (new Rect (2, 10, 128, 32), "Choose Avatar!")) {
StartCoroutine(LoadTexture ());
}
}
IEnumerator LoadTexture ()
{
WWW www = new WWW ("file://"+filelocationinput);;
yield return www;
Texture avatarImage = null;
if (www.error == null && www.texture != null) {
avatarImage = www.texture;
}
else {
avatarImage = texNotFound;
Debug.Log ("Texture not found");
}
renderer.material.mainTexture = avatarImage;
}
}
something like this work. attach a material to the object you want to have the avatar texture on.