• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Aspect13 · Jan 16, 2019 at 04:35 PM · wwwdownloadwebrequestdownloadingftp

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");

 






Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Aspect13 · Jan 17, 2019 at 05:33 PM

Help, please.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Not able to download the same file simultaneously 0 Answers

Why does nothing happen after yield return www 0 Answers

WWW Downloaded file 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges