Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by franky303 · Sep 24, 2012 at 10:02 PM · wwwdownload

Downloading big files on iOS ... WWW will give "out of memory" error.

Hello Folks,

i'm currently working on an unity based iOS project. so far i've overcome many problems (event handling for overlapping buttons, overlapping scrollviews, etc. etc.) ... Now there's another nice problem to be solved:

i'm doing a sync between offline SQlite and online mySQL database (working nicely), as well as downloading many, many files. some of them are videos with big file sizes, for example. those files are too big to be kept in memory completely. so even when i'm happily calling WWW.dispose after completing a filedownload (and the memory consumption is going down again, at least according to System.GC.GetTotalMemory(false), ... there is obviously always a point where i'm running into "out of memory" error because the file to be downloaded is bigger than the available memory.

How would you guys implement a file download which is opening a connection, downloading a few kilobytes (and immediately append to the downloaded file, until it's completed). I was checking out some c# stream based download functions, some of them were using System.Uri which was not available in unity (for both subset and full 2.0 player settings) ...

Any advice ?

Comment
Add comment · Show 2
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 whydoidoit · Sep 24, 2012 at 10:18 PM 0
Share

You would need to consider doing something directly with System.NET - check out this

avatar image franky303 · Sep 25, 2012 at 05:38 PM 0
Share

Yep this is what i've done now. - Works! Using sockets to create a HTTP request and a BinaryReader to save buffer-by-buffer to disk. - Prototype is working.

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by franky303 · Sep 25, 2012 at 05:39 PM

 // Based on this:
 // http://stackoverflow.com/questions/4768443/downloading-file-via-tcpclient-from-http-server
 
 public class main : MonoBehaviour 
 {
     string host = "www.server.com";
     string uri = "/path/to/file/1920x1080.mp4";
     uint contentLength;
     int n = 0;
     int read = 0;
     
     NetworkStream networkStream; 
     FileStream fileStream;
     Socket client;
 
     // Use this for initialization
     void Start () 
     {
         string query = "GET " + uri.Replace(" ", "%20") + " HTTP/1.1\r\n" +
                          "Host: " + host + "\r\n" +
                          "User-Agent: undefined\r\n" +
                          "Connection: close\r\n"+
                          "\r\n";
 
         
         Debug.Log (query);
         
         client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
         client.Connect(host, 80);   
         
         networkStream = new NetworkStream(client);
         
         var bytes = Encoding.Default.GetBytes(query);
         networkStream.Write(bytes, 0, bytes.Length);
         
         var bReader = new BinaryReader(networkStream, Encoding.Default);
         
         string response = "";
         string line;
         char c;
         
         do 
         {
             line = "";
             c = '\u0000';
             while (true) 
             {
                  c = bReader.ReadChar();
                   if (c == '\r')
                        break;
                   line += c;
             }
             c = bReader.ReadChar();
             response += line + "\r\n";
         } 
         while (line.Length > 0);  
         
         Debug.Log ( response );
         
         Regex reContentLength = new Regex(@"(?<=Content-Length:\s)\d+", RegexOptions.IgnoreCase);
         contentLength = uint.Parse(reContentLength.Match(response).Value);
         
         fileStream = new FileStream( Application.persistentDataPath + "/download.mp4", FileMode.Create);
     }
 
     
     void Update()
     {
         byte[] buffer = new byte[4 * 1024];
         
         if (n < contentLength) 
         {
                 if (networkStream.DataAvailable) 
                 {
                   read = networkStream.Read(buffer, 0, buffer.Length);
                   n += read;
                   fileStream.Write(buffer, 0, read);
                 }
                 Debug.Log ( "Downloaded: " + n + " of " + contentLength + " bytes ..." );
          }
         else
         {
               fileStream.Flush();
               fileStream.Close();
             
               client.Close();
         }
     }
     
 }
Comment
Add comment · Show 4 · 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 whydoidoit · Sep 25, 2012 at 06:49 PM 0
Share

You should tick your answer :)

avatar image samcsss · Sep 29, 2014 at 05:26 AM 0
Share

Tried to implement this - and it does work... if you're not using a proxy. Currently Im searching for a solution that will work if a proxy server is set

avatar image foyleman · Oct 01, 2014 at 08:26 PM 0
Share

Using this script (which is great btw)... what happens if the datastream is interrupted?

During the statement if (n < contentLength) there doesn't appear to be a check to see if the the stream stops before contentLength is met.

Or am I wrong to think this.. because the datastream has already been read and therefore contentLength cannot be wrong?

I'm asking because I think I have run into a couple instances of an endless loop here.

avatar image Trungdv · Jan 27, 2016 at 05:58 AM 0
Share

How about https which need user name and password? Can we use this method?

avatar image
0

Answer by franky303 · Oct 07, 2012 at 12:35 PM

Another alternative: Systme.Net.WebClient

Comment
Add comment · Show 3 · 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 whydoidoit · Oct 07, 2012 at 01:53 PM 0
Share

Does that work on iOS now? Never used to.

avatar image DeveshPandey · Jun 26, 2017 at 03:10 PM 0
Share

Here is a plugin on assets store to download big files (in GBs), its working on all promised platform without any problem. http://unitydevelopers.blogspot.in/2017/06/large-file-downloader-for-unity3d.html

avatar image DeveshPandey DeveshPandey · Oct 16, 2017 at 05:17 PM 0
Share

This is the plugin link

https://assetstore.unity.com/packages/tools/network/large-file-downloader-cross-platform-92128

avatar image
0

Answer by MCLD3D · May 21, 2017 at 02:49 AM

If you can explain me how to make this snippet compatible with ipv6, you are a life saver

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

13 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

Related Questions

How to stop a WWW which is in progress? 0 Answers

downloading data in background without halting application 1 Answer

CloudFront statistics show 25% incomplete downloads using Unity3D WWW class 0 Answers

DownloadHandlerAudioClip compressed and streamAudio properties 0 Answers

www.texture memory leak 1 Answer


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