How do I download a file with a non-HTML content type from within a browser?
-
21-08-2019 - |
Question
I'm trying to use content negotiation to give both a HTML and a RDF/XML representation of a resource on a HTTP server. On the server side this works, i.e.
curl -H "Accept: application/rdf+xml" http://localhost:8182/ontologies/1
will retrieve the correct version. I can also do the same with JavaScript/Dojo:
function downloadOntologyRDF(ontologyId) {
dojo.xhrGet( {
url:"${baseUrl}/ontologies/" + ontologyId,
headers: {"Accept": "application/rdf+xml"},
timeout: 5000,
load: function(response, ioArgs) {
var preNode = document.createElement("pre");
preNode.appendChild(document.createTextNode(response));
var foo = new dijit.Dialog({
title: "RDF",
content: preNode,
style: "overflow: auto;"
});
foo.show();
return response;
},
error: function(response, ioArgs) {
alert("Retrieving the RDF version failed: " + response);
return response;
}
});
}
which will display the result in a popup dialog. The point where I am stuck is offering a way to the user to download this version. I would like to have a link on the page that either displays the RDF as a page in the browser or directly opens the save dialog. Is this possible at all without resorting to query parameters or other tricks?
Solution
Like cobbal mentions - since you can not set Accept header in the URL itself, you should have additional content negotiation mechanism. Some frameworks allow content type to be set in form
http://example.com/resource;format
Having the format
at the end of URL, separated with semicolon. Then, when processing request, it parses out the format part.
In your case it could be something like
http://localhost:8182/ontologies/1;rdf
to server rdf, and no format specified to serve whatever is in your accept headers.
OTHER TIPS
This page explains how to setup an aspx page that will show the RDF in the browser. It may help you. Although I can't be sure because you haven's specified what you are running on your server.