96

Today I checked mega.co.nz and I'm excited about some features. For example in download page it will download files on browser and after that decrypt them with javascript.

for example see this link to download a png file :

https://mega.co.nz/#!7JRgFJzJ!efpJGWuPhYczLexY19ex82nuwfs4sR_DG4JXddeClH4

in this link it will start the download inside the browser. i checked network tab in inspect element it will download parts of file with AJAX after that completed all parts of file, will save all of them in one file on computer automatically!

i want to know what they do? can you explain or link to some resource about download files inside browser like that?

also can done it only with javascript or should use some flash plugins or something like that?

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
Amir Molaa
  • 1,103
  • 1
  • 9
  • 9
  • 3
    Yeah, and if you download a large file the system slows down to a crawl because of the constant paging – kinokijuf Apr 15 '14 at 21:07

4 Answers4

107

Mega uses several different methods to do this: (as of 27 Nov 2013)

  1. Filesystem API (Chrome/Firefox Extension polyfill)
  2. Adobe Flash SWF Filewriter (old browsers fallback)
  3. BlobBuilder (IE10/IE11)
  4. MEGA Firefox Extension (deprecated)
  5. Arraybuffer/Blob (in memory) + a[download] (for browsers that support a[download])
  6. MediaSource (experimental streaming solution)
  7. Blob stored in IndexedDB storage + a[download] (Firefox 20+, improvement over the in-memory Blob method)

(source: https://eu.static.mega.co.nz/js/download_6.js)

roberto
  • 3,553
  • 2
  • 27
  • 30
  • but i think it's works on firefox too. i found a sample here http://jsfiddle.net/kGLnP/5/. this sample works on chrome and firefox correctly. but what they do for other browsers like safari or opera? i can download mega.co.nz links with these browsers too.. – Amir Molaa Apr 14 '13 at 01:58
  • 1
    Yes, the sample you provided (http://jsfiddle.net/kGLnP/5/) should work for Firefox, but only the latest ones (version 20 or later, see https://developer.mozilla.org/en-US/docs/HTML/Element/a for more info about supports). I've just read Mega's JavaScript code and updated my answer. (It will answer your question about other browsers. Basically, they will resort to using flash when they cannot use JS-only solutions.) – roberto Apr 14 '13 at 10:38
  • Wow I just saw this site yesterday and was going to ask the same question. When I tried to download a Game mod I was surprised the download start immediately inside the browser showing my full speed rate !!! I was using FF 20.0.1. – zac Apr 16 '13 at 12:45
  • Is roberto's answer still up to date? I've tried downloading a big file from mega.co.nz in firefox and disabled flash and it still -worked. When trying to download a *big file* firefox is asking for approval to store more than 50MB which makes me think they use indexedDB. – shacharz Sep 23 '13 at 14:46
  • 1
    No, it's not (really) up to date, the current download script is here: https://eu.static.mega.co.nz/download_46.js. I took a quick look and it's still more or less the same tricks with some new additions, including performance improvements by caching (using IndexedDB, as you've guessed). I'll update my answer when I have time to read the code. – roberto Sep 23 '13 at 17:48
  • Do you know any JS/Jquery plugin that allows cross-browser download using javascript? – Niros Jan 02 '14 at 15:22
  • @Jimbo: Pretty much, yes. The most recent version is apparently this one: https://eu.static.mega.co.nz/js/download_23.js – roberto Feb 04 '14 at 22:37
  • `download_23.js` is the more recent version of `download_46.js`? They also have spaghetti versioning? – Federico Poloni Feb 26 '15 at 09:43
  • Well, they put them in different directories! (/download_46.js vs /js/download_23.js) ;) – roberto Feb 26 '15 at 10:33
5

A basic implementation of multipart in-browser downloader using Blob and URL APIs is brought here. It downloads a file on 4 concurrent requests and shows the progress also. Please note that it seems setting range header might generally not a good idea on XHR requests, have a look at this topic.

While downloading:

While downloading

After the download:

After the download

Another interesting topic would be implementing Pause/Resume functionality from Mega. XHR API of current browsers doesn't offer that capability so the only chance you have is to do multiple small sized chunks downloading and giving up on the downloaded part of your small chunks, the way it seems is done on Mega also. But fetch streaming feature can be used for that purpose, I didn't explore that yet well enough but it is documented here.

Btw, have a look at these awesome projects:

Ebrahim Byagowi
  • 10,338
  • 4
  • 70
  • 81
1

MEGAcmd

There is megacmd, the official command line interface. You can also build it from sources on github at https://github.com/meganz/MEGAcmd

megacmd is a wrapper around Mega SDK and if you decide to compile it on your own you'll need the same dependencies (on ubuntu) as the ones listed below for Mega SDK.

For details on usage see the MEGAcmd User Guide.

Mega SDK

Mega SDK which can be compiled by following the steps on the github page. It includes the megacli utility which is an interactive shell for synching and downloading/uploading.

## compilation steps for ubuntu
git clone --depth 1 https://github.com/meganz/sdk megasdk
cd megasdk
sudo apt install libcurl4-openssl-dev  libc-ares-dev libssl-dev libcrypto++-dev  zlib1g-dev libsqlite3-dev  libfreeimage-dev libswscale-dev
autogen.sh
./configure
make -j 8  ## pass the number of CPUs you have to speed up compilation
sudo make install

mega.py python module (deprecated)

For those who found this question searching for an actual recipe to download a link in text mode here is a simple python script that uses the mega.py module (install it with sudo pip install mega.py):

import sys
import getpass
#install the module with: 'sudo pip install mega.py'
from mega import Mega

email = '_your_megamail_@domain.com'
password = getpass.getpass(prompt='Mega password for {}:'.format(email))

mega = Mega({'verbose': True})
m = mega.login(email, password)
m.download_url(sys.argv[1])

The script works with python 2.7 and takes the URL of the mega.nz link.

getpass is used for securely entering the password in the console in order to avoid storing the password in the script — if you are comfortable hardcoding the password then set it in line #7.

megatools

On most Linux/posix boxes you can install megatools from standard repositories, i.e.

On ubuntu/debian:

apt install megatools

On MacOS:

brew install megatools

Once installed you will find a number of command line utilities, among which megadl which can download both shared files and your own files. See megadl -h for details.

ccpizza
  • 28,968
  • 18
  • 162
  • 169
0

As of 2020, you can use the Service Workers for seamlessly integrating your custom code with the browser's built-in download manager: https://developers.google.com/web/updates/2016/06/sw-readablestreams

I also guess you'd have the following headers in order for a file to be downloaded instead of being viewed:

 headers: {
   'Content-Type': 'application/octet-stream',
   'Content-Disposition': 'attachment; filename="your_filename.bin"',
 }

Personally I have found this approach to be working flawlessly in both Google Chrome in Firefox, and I'm already using it in production.

vdudouyt
  • 843
  • 7
  • 14