1

I have a cordova - ionic application. I want to download a file from webservice and the file may be any type(JPG,PDG,DOCX etc). I cannot download the file from direct URL. So the app is taking byte array of the file from Webservice.

Anybody know how to download the file in Mobile from the Byte Array. Please help me.

Anu
  • 93
  • 2
  • 9
  • From webservice what you get? image path? – Paresh Gami Feb 15 '16 at 06:33
  • No. IN webservice I am converting the file to byte array string. Then sending the string to mobile app. In mobile app i want to save(download) the file. – Anu Feb 15 '16 at 06:40
  • Instead of convert file to byte array, you can download file from url only – Paresh Gami Feb 15 '16 at 06:42
  • Try fileTransfer plugin http://ngcordova.com/docs/plugins/fileTransfer/ – Paresh Gami Feb 15 '16 at 06:44
  • Dear friend. I cannot do that due to the client's requirement. Thats why I clearly mentioned in my question like this 'I cannot download the file from direct URL'. I already know how to download from the URL. – Anu Feb 15 '16 at 06:44
  • ok sorry. this should help you http://stackoverflow.com/questions/27946228/file-download-a-byte-array-as-a-file-in-javascript-extjs – Paresh Gami Feb 15 '16 at 06:49
  • thanks. but not working. its cordova-ionic app and ipad. – Anu Feb 15 '16 at 07:27
  • can you provide dummy url where i can get byte string so may be i can help you. – Paresh Gami Feb 15 '16 at 08:30

1 Answers1

4

you can use the cordova-plugin-file and the cordova-plugin-file-opener2 plugin.

https://github.com/apache/cordova-plugin-file

https://github.com/pwlin/cordova-plugin-file-opener2

in function with the webservice your code should look like that:

var bytes = new Uint8Array(data.d);
app.writePDFToFile(fileName.split(fileName.split, bytes);

and here is teh function forcing the download:

writePDFToFile: function (fileName, data) {

try {
    window.resolveLocalFileSystemURL(cordova.file.externalApplicationStorageDirectory, function (directoryEntry) {
        directoryEntry.getFile(fileName, { create: true }, function (fileEntry) {

            fileEntry.createWriter(function (fileWriter) {
                fileWriter.onwriteend = function (e) {

                    //window.open(cordova.file.externalApplicationStorageDirectory + fileName, '_system', 'location=yes');

                    cordova.plugins.fileOpener2.open(cordova.file.externalApplicationStorageDirectory + fileName, 'application/pdf',
                        {
                            error: function (e) {
                                console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
                            },
                            success: function () {
                                console.log('file opened successfully');
                            }
                        }
                    );
                };

                fileWriter.onerror = function (e) {
                    alert(e);
                };

                var blob = new Blob([data], { type: 'application/pdf' });

                fileWriter.write(blob);

            }, function onerror(e) {
                alert(e);
            });
        }, function onerror(e) {

            alert(e);
        });
    }, function onerror(e) {            
        alert(e);
    });

} catch (e) {
     alert(e);
}
},

Hope this will help!

deru
  • 460
  • 1
  • 4
  • 15
  • I tried this and can save the file now. But another Error when open the file using Acrobat reader app 'The document cannot be opened because it is not a valid pdf file'. Below the code: In web service(C#): string file_path=“…path to the pdf file..”; string data_to_send_to_mobile=System.Text.Encoding.UTF8.GetString(File.ReadAllBytes(file_path)); In Mobile(Javascript) : var str=data_to_send_to_mobile; var data = []; for (var i = 0; i < str.length; ++i) { data.push(str.charCodeAt(i)); } var fileName=‘any_name.pdf’; window.resolveLocalFileSystemURL( …. – Anu Feb 16 '16 at 05:51
  • Solved it! I used base64string to take from webservice. Thanks deru. – Anu Feb 16 '16 at 06:46