- Home /
DownloadHandlerAssetBundle from password protected ftp server on Android
Is it possible to use the DownloadHandlerAssetBundle (UnityWebRequest) to download from a password protected ftp server on Android?
I got it working on PC by using this format for the url: ftp://username:password@hostname/
But it looks like that isn't supported on Android. I ended up trying to use SetRequestHeader("AUTHORIZATION", "Basic" + base64UsernameAndPassword). But that wouldn't even work on PC. I'm guessing that's not part of the ftp protocol.
I've tried searching for the solution but came up empty.
Answer by _NullPointer_ · Jan 31, 2017 at 06:24 PM
I had your same problem. The Doc says that you can only use UnityWebRequest on anonimous ftps, so I finally ended up using .net FtpWebRequest for pre-downloading bundles to disk and after that load them using www:
private void downloadFileToDir( string file, string dir) {
try {
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ip + dir + '/' + file));
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
//ftpRequest.UsePassive = true;
ftpRequest.UseBinary = true;
ftpRequest.KeepAlive = false;
ftpRequest.Credentials = new NetworkCredential(userName, password);
FtpWebResponse responseFileDownload = (FtpWebResponse)ftpRequest.GetResponse();
Stream responseStream = responseFileDownload.GetResponseStream();
FileStream writeStream = new FileStream(Application.persistentDataPath + "/" + dir + "/" + file, FileMode.Create);
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0) {
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
writeStream.Close();
responseFileDownload.Close();
}
catch (Exception e) {
Debug.Log("Could not download file " + dir + '/' + file + "/n" + e.ToString());
}
}
For loading the bundles:
using (WWW www = new WWW("file://" + Application.persistentDataPath + "/yourRoute/yourAssetBundle/")) {
yield return www;
if (www.error != null)
throw new Exception("WWW download had an error:" + www.error);
AssetBundle bundle = www.assetBundle;
....
Your answer
Follow this Question
Related Questions
Is the UnityWebRequest header of Android build of Unity2019 correct? 0 Answers
FTP error only in non-development builds 1 Answer
Why am I unable to locate font files(.ttf) in unity android game in persistentDataPath? 1 Answer
Using AssetBundles throughout project 0 Answers
[Android] Escape characters for the password of my alias key 0 Answers