Quantcast
Channel: Questions in topic: "webrequest"
Viewing all articles
Browse latest Browse all 387

Download progress is -1 forever.

$
0
0
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().downloadWithFTP("ftp://dfpsgamehost@files.000webhost.com/testfile.zip", path, "login", "password");

Viewing all articles
Browse latest Browse all 387

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>