15
Views
2
Comments
Solved
How to download pdf file in Mobile application
Question
Application Type
Mobile

I want to download pdf/word file in mobile application for that i am  using this java script. So, I only able to download in chrome browser in my laptop. when I try to download file in mobile with this java script code it is running properly but not downloading file.

// Decode the Base64 string to binary data

var byteCharacters = atob($parameters.Base64);


// Create an array of byte numbers from the binary data

var byteNumbers = new Array(byteCharacters.length);

for (var i = 0; i < byteCharacters.length; i++) {

    byteNumbers[i] = byteCharacters.charCodeAt(i);

}


// Convert the byte array to a Uint8Array

var byteArray = new Uint8Array(byteNumbers);


// Create a Blob with the appropriate MIME type

var blob = new Blob([byteArray], { type: $parameters.ContentType });


// Generate a temporary URL for the Blob

var url = URL.createObjectURL(blob);


// Create a temporary anchor element to trigger the download

var a = document.createElement("a");

a.href = url; // Set the href attribute to the Blob URL

a.download = $parameters.Filename; // Set the filename for the download


// Programmatically click the anchor to start the download

document.body.appendChild(a); // Append the anchor to the DOM (necessary for some browsers)

a.click(); // Trigger the download

document.body.removeChild(a); // Remove the anchor after the download


// Revoke the Blob URL to free up memory

URL.revokeObjectURL(url);

2020-07-21 19-28-50
Rajat Agrawal
Champion
Solution

Hi @Neha Gupta,

As per you question what i understand is that you nee to download file from mobile app.

In the mobile, we have cordova plugin that save file or we can say download file.

By using this we can call javascript to run this plugin.

Below is the syntax for javascript:

function fixBinary (base64Str) {

    var base64Array = base64Str.split(",");

    var base64Raw = base64Array.length > 1 ? base64Array[1] : base64Array[0];

    var bin = atob(base64Raw);

    var length = bin.length;

    var buf = new ArrayBuffer(length);

    var arr = new Uint8Array(buf);  

    for (var i = 0; i < length; i++) {

      arr[i] = bin.charCodeAt(i);

    }   

    return buf;

}

var binary = fixBinary($parameters.FileContent);

var blob = new Blob([binary], {type: $parameters.FileType});

cordova.plugins.saveDialog.saveFile(blob, $parameters.FileName).then(function() {

    $parameters.Success = true;

    $resolve();

}).catch(function(reason){

    $parameters.Success = false;

    $parameters.ErrorMsg = reason;

    $resolve();

});

And this the link for cordova plugin:

https://github.com/aliitotz/cordova-plugin-save-dialog.git


Hope this will helps you!!


Regards,

Rajat

2024-06-07 07-10-45
Leon Richard

This JavaScript code is running correct and the file is also downloading. Thank you for replying so quickly. This is helpful for me.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.