unity3d - Download files via FTP on Android -


i need make connection local ftp protocol between computer (server) , android device (client). should download files (images, obj,...) used in android unity app scene. i've used www class create connection , works fine in unity player run in computer client. once i've exported same scene android apk didn't work (i'm sure ftp connection stable , works because i'm able access files browser). know if there way or there problems in code use ftp protocol on android unity app? (the client doesn't need authorisation , authentication anonymous) here code use download 1 image inside scene , render sprite.

 using system.collections;  using system.collections.generic;  using unityengine;  using system.net;  using system.io;   public class clientftp : monobehaviour  {    public unityengine.ui.image label;     ienumerator start ()    {        // create connection , whait until established        string url = ("ftp://192.168.10.11/prova.png");        www ftpconnection = new www (url);        yield return ftpconnection;        // download image , render texture        texture2d tex = new texture2d (250, 192);        ftpconnection.loadimageintotexture (tex);        // assign texture new sprite        sprite s = sprite.create (tex, new rect (0, 0, 250f, 192f), new vector2 (0.5f, 0.5f), 300);        label.preserveaspect = true;        label.sprite = s;      }  } 

why use ftp if don't need credential access files? can place files in server access them www or unitywebrequest api.

to answer ftp question, www not meant used ftp protocol. ftpwebrequest api used for.

below sample of ftpwebrequest.

private byte[] downloadwithftp(string ftpurl, string savepath = "", string username = "", string password = "") {     ftpwebrequest request = (ftpwebrequest)webrequest.create(new uri(ftpurl));     //request.proxy = null;      request.usepassive = true;     request.usebinary = true;     request.keepalive = true;      //if username or password not null use credential     if (!string.isnullorempty(username) && !string.isnullorempty(password))     {         request.credentials = new networkcredential(username, password);     }      request.method = webrequestmethods.ftp.downloadfile;      //if savepath not null, want save file path     //if path null, want return file array     if (!string.isnullorempty(savepath))     {         downloadandsave(request.getresponse(), savepath);         return null;     }     else     {         return downloadasbytearray(request.getresponse());     } }  byte[] downloadasbytearray(webresponse request) {     using (stream input = request.getresponsestream())     {         byte[] buffer = new byte[16 * 1024];         using (memorystream ms = new memorystream())         {             int read;             while (input.canread && (read = input.read(buffer, 0, buffer.length)) > 0)             {                 ms.write(buffer, 0, read);             }             return ms.toarray();         }     } }  void downloadandsave(webresponse request, string savepath) {     stream reader = request.getresponsestream();      //create directory if not exist     if (!directory.exists(path.getdirectoryname(savepath)))     {         directory.createdirectory(path.getdirectoryname(savepath));     }      filestream filestream = new filestream(savepath, filemode.create);       int bytesread = 0;     byte[] buffer = new byte[2048];      while (true)     {         bytesread = reader.read(buffer, 0, buffer.length);          if (bytesread == 0)             break;          filestream.write(buffer, 0, bytesread);     }     filestream.close(); } 

usage:

download , save(no credential):

string path = path.combine(application.persistentdatapath, "ftp files"); path = path.combine(path, "data.png"); downloadwithftp("ftp://yoururl.com/yourfile", path); 

download , save(with credential):

string path = path.combine(application.persistentdatapath, "ftp files"); path = path.combine(path, "data.png"); downloadwithftp("ftp://yoururl.com/yourfile", path, "username", "password"); 

download (no credential):

string path = path.combine(application.persistentdatapath, "ftp files"); path = path.combine(path, "data.png"); byte[] yourimage = downloadwithftp("ftp://yoururl.com/yourfile", "");  //convert sprite texture2d tex = new texture2d(250, 192); tex.loadimage(yourimage); sprite s = sprite.create(tex, new rect(0, 0, texture2d.width, texture2d.height), new vector2(0.5f, 0.5f)); 

download (with credential):

string path = path.combine(application.persistentdatapath, "ftp files"); path = path.combine(path, "data.png"); byte[] yourimage = downloadwithftp("ftp://yoururl.com/yourfile", "", "username", "password");  //convert sprite texture2d tex = new texture2d(250, 192); tex.loadimage(yourimage); sprite s = sprite.create(tex, new rect(0, 0, texture2d.width, texture2d.height), new vector2(0.5f, 0.5f)); 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -