- Home /
Uploading file on Android
Hi! I am trying to upload file by WWWForm on my android device. Code worki in Unity3d editor and it uploads with no problem (I have ongui progress printed). When I run my app on my phone I select the file and my progress shows only '0%' then '100%' (like split secound). Offcourse file is not uploaded. I am using sample code:
IEnumerator UploadFileCo(string localFileName, string uploadURL)
{
if(once==false){
WWW localFile = new WWW("file:///" + localFileName);
once=true;
yield return localFile;
if (localFile.error == null){
Debug.Log("Loaded file successfully");
}
else
{
Debug.Log("Open file error: "+localFile.error);
yield break;
}
WWWForm postForm = new WWWForm();
postForm.AddBinaryData("theFile",localFile.bytes,"localFileName","video/3gpp");
upload = new WWW(uploadURL,postForm);
yield return upload;
if (upload.error == null){
Debug.Log("upload done :" + upload.text);
}
else
{
Debug.Log("Error during upload: " + upload.error);
}
}
}
What I am doing wrong? Or is it my phone? I have manifest white permitions to access internet. My Android version 2.3.5. Also I am uploading .3gp file. Thanks for any advice.
I'm wondering if your path to the file is correct on the phone?
Answer by Bunny83 · Oct 24, 2012 at 08:59 PM
Yep, like BerggreenDK said, have you pinned the problem down to the sending part? Are you sure the loading works? Just do some debugging. A quick and easy way to display debug information is to use a script like this:
// MobileDebug.cs
using UnityEngine;
public class MobileDebug : MonoBehaviour
{
private Vector2 m_ScrollPos;
private static string m_DebugText = "";
public static void Log(string aText)
{
m_DebugText += aText + "\n";
}
void OnGUI()
{
GUILayout.BeginArea(new Rect(0,0,400,400));
m_ScrollPos = GUILayout.BeginScrollView(m_ScrollPos);
GUILayout.Label(m_DebugText);
GUILayout.EndScrollView();
GUILayout.EndArea();
}
}
Just attach this script to any GO in your scene (that isn't going to be destroyed) and instead of "Debug.Log();" or "print();" you use "MobileDebug.Log();".
Place some logs at meaningful places to see what happens. Make sure you also log the ".error" of the WWW object when it fails.
You might need to adjust the placement of the GUI.
Answer by kamesenin · Oct 25, 2012 at 10:32 AM
Thanks for answer but I am way pass that. My path to file was correct . I didn't pin point why it doesn't work, but came with other solution. Using FtpWebRequest:
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftp+namex+".jpg"));
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
// this.ftpRequest.Proxy = null;
ftpRequest.UsePassive = true;
ftpRequest.UseBinary = true;
ftpRequest.Credentials = new NetworkCredential("xxx@xyz.com", "qwerty");
FileInfo ff = new FileInfo(thumpath);
byte[] fileContents = new byte[ff.Length];
using (FileStream fr = ff.OpenRead())
{
fr.Read(fileContents, 0, Convert.ToInt32(ff.Length));
done=true;
}
using (Stream writer = ftpRequest.GetRequestStream())
{
writer.Write(fileContents, 0, fileContents.Length);
}
where "thumbpath" is my file path, "namex" is file name and "ftp" is ftp address. Login and password are pass in "NetworkCredential" . For me there were two disadvantages: 1. Not everyone have ftp , 2. When uploading, Unity app freezes but after upload everthing worked normal. Now I am using asset know as 'boxit' and it's perfect for my project. Hope that this post will help someone. Cheers!
Your answer

Follow this Question
Related Questions
Is there a way to fix uploadprogress on mobile? 1 Answer
Help with saving XML files with PHP to a server 0 Answers
Upload Game For Beta 2 Answers
How to upload/download video from/to mobile 1 Answer
Upload large files 0 Answers