Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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

2 Replies

· 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
avatar image
0

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 .

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.

Follow this Question

Answers Answers and Comments

101 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 avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges