- Home /
How do I Download FTPES Password Protected File in Background
I've been messing around with a lot of things in the last couple of weeks and can't seem to find the right way of doing this.. I'm doing this in Unity, but of course the essence should be maintainable within an environment.
I have 3 audio files and a Text Document on an FTPES that is to be clear Explicit TLS over FTP:
I have successfully logged in using the following code to verify the file's existence:
WebRequest.RegisterPrefix("ftps", new FtpsWebRequestCreator());
ftpRequest = (FtpWebRequest)WebRequest.Create("ftps://" + IP + "/" + fileEndPath);
ftpRequest.Credentials = new NetworkCredential(User, Password);
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateCertificate);
ServicePointManager.Expect100Continue = true;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpResponse.Close();
ValidConnection = true;
This being said, I have also successfully downloaded the file a number of ways.. the issues I've had are as follows:
Using FtpWebRequest I can download file and used to be able to load the file as an AudioClip (I honestly don't know why it randomly stopped working, the downloading using ftpwebrequest that is...) using the WWW class, but because I could not seem to find a way to shove it into the background.. I tried writing to file using streamWriter, and it didn't seem to work.
Using UnityWebRequest I cannot log into a FTPS server as it is designed for HTTP based communications.
- Using WebClient I was able to log in to the sever (by registering the prefix as shown above and associating it on the low level with the useSsl bool set to true) and
Using .DownloadFile not able to download file, hangs Unity Main Thread.
Using .DownloadFileAsyn able to download the the file, but have no read permission, and Complete event never gets called to continue into loading the file using WWW.
Where I am as of this post is using WebClient.DownloadFileAsyn:
internal void Download(FileType fileType)
{
IsDownloading = true;
if (ValidConnection)
{
try
{
StartCoroutine(SetFileCreation());
using (WebClient client = new WebClient())
{
WebRequest.RegisterPrefix("ftps", new FtpsWebRequestCreator());
client.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadFileProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
client.DownloadFileAsync(ftpIP, Application.dataPath + "/StreamingAssets/" + ftpFilePath, fileType);
}
}
catch (WebException ex)
{
Debug.LogError(ex.Message + ex.StackTrace + "\n");
}
}
else
IsDownloading = false;
}
SetFileCreation(); is a method which verifies the location, and if it exists, overwrites, otherwise creates the directories before continuing.
Now I don't have any clean code (other than what I've shown) to show what I've been doing, but if some one could just either point me to a good example of this, (I've yet to find much on downloading from FTPS in Unity) or just share how they'd approach this issue from the ground up, it'd be much appreciated.
As a side note, I know my FTP server is functioning as intended via https://ftptest.net/ And my Server Logs (I host it myself, so I've full control of settings) show all correct messages all up to the RETR and Transfer Successful messages.
I've seen plenty of posts on FTPS, on Various ways to download inside of Unity and Outside, all the different levels of network communication; high level and low level, and how to load files from a folder into Unity in realtime. But nothing covering it all in one, nice and clean. KISS!
As most of us, I've got the bigger picture to focus on and can't let this detail continue to delay development.
Edit (Server Logs):
Here's the Server log from my WebClient.DownloadAsync();
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> Connected, sending welcome message...
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> AUTH TLS
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> 234 Using authentication type TLS
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> SSL connection established
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> PBSZ 0
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> 200 PBSZ=0
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> PROT P
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> 200 Protection level set to P
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> USER Test
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> 331 Password required for test
(000657)08/23/2018 22:51:32 - (not logged in) ([MyIP])> PASS ********
(000657)08/23/2018 22:51:32 - test ([MyIP])> 230 Logged on
(000657)08/23/2018 22:51:32 - test ([MyIP])> OPTS utf8 on
(000657)08/23/2018 22:51:32 - test ([MyIP])> 200 UTF8 mode enabled
(000657)08/23/2018 22:51:32 - test ([MyIP])> PWD
(000657)08/23/2018 22:51:32 - test ([MyIP])> 257 "/" is current directory.
(000657)08/23/2018 22:51:32 - test ([MyIP])> CWD /
(000657)08/23/2018 22:51:32 - test ([MyIP])> 250 CWD successful. "/" is current directory.
(000657)08/23/2018 22:51:32 - test ([MyIP])> TYPE I
(000657)08/23/2018 22:51:32 - test ([MyIP])> 200 Type set to I
(000657)08/23/2018 22:51:32 - test ([MyIP])> PASV
(000657)08/23/2018 22:51:32 - test ([MyIP])> 227 Entering Passive Mode ([MyIP],195,146)
(000657)08/23/2018 22:51:32 - test ([MyIP])> RETR MainQuests.txt
(000657)08/23/2018 22:51:32 - test ([MyIP])> 150 Connection accepted
(000657)08/23/2018 22:51:32 - test ([MyIP])> SSL connection for data connection established
(000657)08/23/2018 22:51:32 - test ([MyIP])> 226 Transfer OK
It's worth noting that the connection is maintained.. it does not close, because the connection is set to close after the file is finished downloading...
Your answer
Follow this Question
Related Questions
How Export and Downloading Asset Bundle at runtime? 0 Answers
How to background download Addressable Assets System in iOS? 0 Answers
Addressables: PercentComplete did not started from 0 and suddenly reach 1 6 Answers
Download binary file from my FTP server 0 Answers
What is the download size of updating from version 2019.3 to 2019.4.1f1? 0 Answers