Search code examples
javascriptjquerycordovaphonegap

Download video from the video tag in my phonegap app which has src


I am adding a download button to download video from my phonegap android app. How to download the video to my storage of android from the video tag in my app

I have to know the optimized way to complete this feature

function videoDownload(){

//find the first element with video tag in current screen
var video = document.getElementsByTagName("video")[0];
//video.src gives url as expected
console.log(video.src);
//show download progress for user
//download video to the storage
const url = video.src
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
// the filename you want
a.download = 'share-video.mp4';
document.body.appendChild(a);
a.click();

}

Thanks in advance


Solution

  • You need to first install cordova-plugin-file-transfer to get started with your file download. Then you need to call the download() of the plugin to download a file. The below is a sample code that can achieve the download, but clearly, you need to provide it with actual URLs for the download to be successful.

    function downloadAsset() {
    
      var fileTransfer = new FileTransfer();
      var assetURL = 'your_video_url'; // is video.src string
      //for android app
      var fileName = '/storage/emulated/0/Android/data/[package-name]/files' + 'new_name'; // is share-video.mp4
    
      fileTransfer.download(assetURL, fileName,
        function(entry) {
          console.log("Success!");
        },
        function(err) {
          console.log("Error");
        });
    }