I’m developing a Cordova application where users can download videos. I’m using the
cordova-plugin-file
plugin to handle downloads. I’ve implemented logic to:
Send a request to a server to initiate the download.
Save the downloaded file in the user-accessible storage (Downloads folder) on the Android device.
Request storage permissions (READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE) before downloading.
The download completes successfully within the app (At least when i’m monitoring the Network section on the console).
However, the downloaded video file is not showing up in the phone’s Downloads folder or any other user-accessible storage location.
Here is my javascript code
$(document).ready(function() {
$('#download-form').submit(function(e) {
e.preventDefault();
// Get form data
var videoUrl = $('#video_url').val();
var resolution = $('#resolution').val();
// Display downloading message
$('#alert-message').removeClass('alert-success alert-danger').text('Downloading...').show();
// Send POST request to the Node.js server
$.ajax({
url: 'https://alpine-almond-cross.glitch.me/download',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({ video_url: videoUrl, resolution: resolution }),
success: function(response) {
if (response.filePath) {
// Download path received, likely download started on server
$('#alert-message').removeClass('alert-danger').addClass('alert-success').text('Download initiated. Please wait...').show();
document.addEventListener('deviceready', function() {
// Here the code using FileTransfer runs after device is ready
function downloadVideo() {
if (window.hasPermission(cordova.plugins.file.PERSMISSION_READ_EXTERNAL_STORAGE) && window.hasPermission(cordova.plugins.file.PERSMISSION_WRITE_EXTERNAL_STORAGE)) {
var fileTransfer = new FileTransfer();
var fileUrl = encodeURI(response.filePath);
var localPath = cordova.file.externalDataDirectory + 'Downloads/download.' + resolution;
fileTransfer.download(
fileUrl,
localPath,
function(entry) {
// Download successful
$('#alert-message').text('File downloaded successfully: ' + entry.toURL()).show();
},
function(error) {
// Download error
$('#alert-message').removeClass('alert-success').addClass('alert-danger').text('Error downloading file: ' + error.source + ' ' + error.target + ' ' + error.code).show();
}
);
} else {
// Permission not granted, handle error
$('#alert-message').removeClass('alert-success').addClass('alert-danger').text('Storage permission denied. Please allow storage access to download videos.');
}
}
// Request storage permissions if needed
cordova.plugins.file.requestPermissions(cordova.file.externalDataDirectory, downloadVideo);
}, false);
} else {
// Error handling: Download path not received
$('#alert-message').removeClass('alert-success').addClass('alert-danger').text('Error initiating download. Please check the video URL and resolution.');
}
},
error: function(xhr, status, error) {
// Display error message
$('#alert-message').removeClass('alert-success').addClass('alert-danger').text(xhr.responseJSON.message);
}
});
});
});
COSYBANANA63 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.