2

I have an Web API written in Core 2 that returns a FileContentResult.

You need to do a GET with an Auth Token in the header for it to work. If you try it with postman, the file renders fine and it also renders well in an MVC View.

Now we have an external Salesforce system trying to consume the API but due to the limitations of the APEX language they have to use Javascript to inject the token into the GET method.

Example code:

$(document).ready(function() {
    $.ajax({
        type: 'GET',
        url: 'https://file-store.com/document/{! docId}',
        headers: {
            'Authorization': 'Bearer {! oauthToken}',
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }
    }).done(function (data) {
        console.log(data);
    });
});

This seems to work as the "data" that is returns contains a file - or at least an array of squiggles that look like the file serialized.

However, there seems to be no way to get the web view to render this.

I tried using embed

 var object = '<embed scr="' + data + '" />';

or using an iframe

 $('#iFrame').html(data);

or

 $('#iFrame').html(object);

and lots of other things but so far had no success.

I do understand that in MVC this is simple, but is it at all feasible using javascript?

We do successfully receive the file, and we do have the data in memory, I just need way to render it on the browser.

Any suggestion is very welcome

Nick
  • 2,745
  • 1
  • 27
  • 53
  • What type of file are your trying to display? – Mark Jun 19 '18 at 18:17
  • You could look into using Web API / blob objects. See: https://developer.mozilla.org/en-US/docs/Web/API/Blob – Mark Jun 19 '18 at 18:18
  • the file could be anything, from image to pdf. The FileContentResult has the content type added in the response header so when you view the result in a browser, it knows to always render the right thing. – Nick Jun 20 '18 at 09:11

0 Answers0