My file can only be served if I add an x-authentication token to the header of the "GET" http request (and I don't want to use a cookie). The direct consequence is that I can not call for a file just by using its URL in the html, I need to get it from a JavaScript call, asynchronous preferably.
Since my client is Meteor, I want to use the HTTP.get method. I get the part where I obtain my file with http.get, but not the one where I attach it to, for example, a src attribute of an image.
How should I return the result of my file download if I want to use it in my page? (e.g.attach it to the src attribute of an image). Should I use a dedicated template or can it be achieved using an helper?
EDIT: I made some progress. Based on the answers to a similar case "how to display images downloaded using XMLHttpRequest, I have seen that you can encode in base64 the response content text.
So here is my thinking: I use an helper to return the img src attribute. I know that the http call is asynch when I use it client-side so I return a placeholder value by default (see the last line)
here is my code (the helper is called inside the img src attribute):
link : function(){
HTTP.get ("http://localhost:3000" + Files.baseURL + "/" + this.md5,
{
headers:{
"X-Auth-Token": Accounts._storedLoginToken()
}
},
function (error, result) {
if (error){
console.log ("an error " + result.statusCode + " occured");
}
else
{
retval ="";
for (var i=0; i<=result.content.length-1; i++)
retval += String.fromCharCode(result.content.charCodeAt(i) & 0xff);
return "data:"+this.contentType+";base64," + encode64(retval)
}
})
return "http://localhost:3000/images/placeholder.png"
},
This code doesn't work, it's throwing an error on the http.get line. "Exception in template helper: .link@http://localhost:3000/client/views/uploader/uploader.js?b82cf5a1d421ef6a5a1e4eabaf8327fd1a9f2d75:43:2"
As BraveKenny advised, I used Chrome instead of Firefox to debug. It appeared that it was just the http package missing.