0

I have a protected API that fetches the contents of a file. By protected I mean, I need to send in Authorization headers before the API will allow me to fetch the file contents.

How do I display this in a browser window?

Currently, my nodejs backend is returning the contents with Content-Type:text/html

On the frontend, my current code looks like this

$http.get(downloadUrl)
  .then(function(resp) {
    var data = resp.data;
    var blob = new Blob([data], { type: "text/html" });
    let objectUrl = (window.URL || window.webkitURL).createObjectURL( blob );

    let anchor = document.createElement("a");
    anchor.href = objectUrl;
    anchor.download = 'source.html';
    anchor.click();

    window.URL.revokeObjectURL(objectUrl);

This just download the file though. I just want to display it, preferably in a new window.

Edit I believe this is a duplicate of this question and there is no front-end-only solution to it. It requires the backend to play a part, such as implementing a "Holder-Of-Key Authentication Scheme"

kane
  • 5,465
  • 6
  • 44
  • 72
  • 1
    `open('source.html')` instead of using a dowload link. – StackSlave Jun 11 '18 at 23:45
  • 1
    open() is a global, it needs no object. nopeneither does URL for that matter. You can say `window.open` if it makes you feel good, but you might as well use `window.window.window.open()` if you're being redundant anyway ;) – dandavis Jun 11 '18 at 23:52
  • window.open() will not work. The file is protected behind the API so I would need to send headers. Unless that can be done with window.open() – kane Jun 12 '18 at 00:47

0 Answers0