- Home /
Download progress is -1 forever.
Hello, I want to download files from my FTP server. The login and password in the code is correct I checked it many times. Also the link to the file is correct I guess. The download code is not my code I found it on the Unity Forum here: https://stackoverflow.com/questions/43411525/download-files-via-ftp-on-android
And the code is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Net;
using System;
using System.Text;
using System.IO.Compression;
public class GameUpdater : MonoBehaviour
{
public 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 is NOT null then use Credential
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
{
request.Credentials = new NetworkCredential(userName, password);
}
request.Method = WebRequestMethods.Ftp.DownloadFile;
//If savePath is NOT null, we want to save the file to path
//If path is null, we just want to return the file as 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 it does 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();
}
}
There is no errors in the downloading script above. I use it like that:
string path = Path.Combine(Application.persistentDataPath, "/temp");
path = Path.Combine(path, "testfile.zip");
GameUpdater.GetComponent<GameUpdater>().downloadWithFTP("ftp://dfpsgamehost@files.000webhost.com/testfile.zip", path, "login", "password");
Answer by florinel2102 · May 17 at 11:24 AM
I had this issue with UnityWebRequest because I used yield return WebRequest;
instead of yield return WebRequest.SendWebRequest();
so make sure that you actually send the request and wait for a response .
Your answer
Follow this Question
Related Questions
Not able to download the same file simultaneously 0 Answers
DownloadHandlerAudioClip compressed and streamAudio properties 0 Answers
See Request of WWW or UnityWebRequest, 403 error only from android in a specific url (GET) 2 Answers
www.texture textureType to GUI 0 Answers
How to constantly get new JSON information with GET request from a stream? Help!! 0 Answers