0

I have a Cordova app that uses a C# webService to communicate with a SQL database. This works great. My problem is that I have some pdf documents on the server with the local filePath held in the database and I need to open these in the app.

I have done a similar thing before where the documents had a URL where they could be reached so they just open, but in this case there is no external access to the file.

So my question is this....how do I best get the file from the server to the app to open it?

I don't need to store the file on the device, just open it so it can be read.

I would be really grateful if someone could steer me in the right direction as I have no clue as the best method for achieving what i'm after.

*****UPDATE******

Right, I don't think i'm a million miles away but have a feeling i'm doing something fundamentally wrong.

I'm creating a byte[] using:

byte[] bytes = System.IO.File.ReadAllBytes(filepath);

which produces a really long string.

In the app, I'm getting that string and using the following to reconstitute it as a file:

var bytes = new Uint8Array(data);
saveByteArray("mytest.txt", data); 


        function base64ToArrayBuffer(base64) {
            var binaryString = window.atob(base64);
            var binaryLen = binaryString.length;
            var bytes = new Uint8Array(binaryLen);
            for (var i = 0; i < binaryLen; i++) {
            var ascii = binaryString.charCodeAt(i);
            bytes[i] = ascii;
            }
            return bytes;
        }

        function saveByteArray(reportName, byte) {
            var blob = new Blob([byte], {type: "application/txt"});
            var link = document.createElement('a');
            link.href = window.URL.createObjectURL(blob);
            var fileName = reportName;
            link.download = fileName;
            link.click();
        }

This will either create an empty file or a corrupt one.

Can anyone help with this please? A fresh pair of eyes would be gratefully received.

Thanks

GImbal
  • 11
  • 3
  • If you don't want the clients have access to the file directly, you should setup a shared file store to which they all have access. I personally don't find this very safe, I would recommend instead enabling users to download a local copy of the file when opening. – Igor Meszaros May 21 '19 at 12:57

1 Answers1

0

What you could do would be to make a new endpoint in your C# webservice backend, to download the file from this endpoint, to store it locally, and to display it from your app.

Behind the endpoint, it would use the file location from the database, and it would get the file content in a stream from where the pdf is stored. This stream of data would be placed in a json result object as an array of bytes. Finally, your app would have to get this json object, then to build the pdf file from the array of bytes and from the file name.

Hope it helps.

Skrface
  • 1,388
  • 1
  • 12
  • 20
  • It certainly gives me me a decent starting point. Thanks for that. – GImbal May 21 '19 at 16:32
  • Right, I don't think i'm a million mile away but have a feeling i'm doing something fundimentally wrong. – GImbal May 24 '19 at 10:08
  • What part makes you feel this way? – Skrface May 24 '19 at 10:27
  • Sorry, I thought I'd removed this comment. I've updated the original question to show where I've got to, but I'm not quite there. I'm doing something not quite right. – GImbal May 24 '19 at 12:09
  • @IanHeathcock You should not edit your question to ask another question, even if it is more or less related. Your initial question was about an advice for an "all solution concept", now it aims to another topic. Next time, please ask another question if you cannot find a solution. That's said, you will find a solution to your current problem here => https://stackoverflow.com/a/35412205/5992860 Don't forget to mark this answer as validated if it did answer correctly to your question. – Skrface May 24 '19 at 12:27
  • Thanks. Will bare that in mid for the future. – GImbal May 24 '19 at 12:53