Press "Enter" to skip to content

How to Download Files Using Javascript | JS File Downloader

JS File Downloader is a simple, no-dependency library that will be able to download the file from the browser and show the download status.

How to make use of it:

1. Install & download the package deal.

# NPM
$ npm install js-file-downloader --save

2. Import the file downloader as a module.

import Downloader from 'js-file-downloader';

3. Or load the js-file-downloader.min.js from the dist folder.

<script src="./dist/js-file-downloader.min.js"></script>

4. Create a new downloader instance the specify the path to the file to be downloaded.

new jsFileDownloader({ 
    url: '1.jpg' 
})

5. Do one thing when the download is ended.

new jsFileDownloader({ 
    url: '1.jpg' 
})
.then(function () {
  alert('File downloaded');
});

6. Do one thing when an error occurred.

new jsFileDownloader({ 
    url: '1.jpg' 
})
.catch(function (error) {
  // do something
});

7. Determine theΒ most time to wait earlier than stopping downloading. Default: β€˜40000’.

new jsFileDownloader({ 
    url: '1.jpg',
    timeout: 50000
});

8. Determine whether or not to disable download on mobile gadgets. Default: true.

new jsFileDownloader({ 
    url: '1.jpg',
    mobileDisabled: false
})

9. Determine whether or not to auto begin downloading on web page load. Default: true.

new jsFileDownloader({ 
    url: '1.jpg',
    autoStart: false
});

10. Determine whether or not to drive desktop mode even on mobile gadgets for downloading information. Default: false.

new jsFileDownloader({ 
    url: '1.jpg',
    forceDesktopMode: true
});

11. Customize the request header data.

new jsFileDownloader({ 
    url: '1.jpg',
    headers: [
      { name: 'Authorization', value: 'Value...' }
    ]
});

12. Customize the file title.

new jsFileDownloader({ 
    url: '',
    filename: ''
});

13. Check download status utilizing the process function.

new jsFileDownloader({ 
    url: '1.jpg',
    process: process
});

function process (event) {
  if (!event.lengthComputable) return;
  var downloadingPercentage = Math.floor(event.loaded / event.total * 100);
  // what to do ...
};

14. Determine the strategy for the HTTP request.

new Downloader({ 
    url: '...',
    method: 'POST' // "GET", "POST", "PUT"
})

15. Customize the ultimate file title with the nameCallback.

new Downloader({ 
    nameCallback: function(name) {
      return 'i-am-prefix-' + name;
    }
})

File Downloader With Promise Support, JS File Downloader Github, File Downloads in the Browser


See Demo And Download

Official Website(AleeeKoi): Click Here

This superior jQuery/javascript plugin is developed by AleeeKoi. For extra Advanced Usages, please go to the official website.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *