Here is the process I am trying to achieve: 1) A browser sends an ajax request to the server, requesting a pdf file. 2) The server downloads the pdf file and returns it for display. 3) The browser then displays the downloaded pdf in an existing iframe.
Below you will find the code I am using. It seems to be encountering an issue at the point of displaying the pdf in the iframe, indicating a potential problem with how the pdf is being sent.
Code in the browser's index.html file:
var uri = '/viewer/loaddrawing/';
$.getJSON(uri, {key:value}, function(data, jqXHR){
document.getElementById("iframetitle").src = uri;
});
Code in the Django server's views.py file:
import requests
def loaddrawing(request):
value = request.GET.get('key')
# The key is used to generate a unique URL, but for testing purposes, we'll use the URL provided below
url = "http://cbmeturkey.com/media/109/test.pdf"
response = urllib2.urlopen(url)
some_data = response.read()
return HttpResponse(some_data, mimetype='application/pdf')
EDIT:
I am facing one final issue: I would prefer not to use the following code in my index.html file because I want to download the pdf once and reuse it. Currently, the pdf is redownloaded each time the iframe is shown, due to the code below.
var uri = '/viewer/loaddrawing/';
document.getElementById('iframetitle').src = uri + '?key=' + value;
SOLVED: The previous issue was resolved with the guidance of Augusto. Below is the updated index.html code that loads the pdf only once. Please note that I am now modifying a div element named "divtitle" instead of an iframe:
var uri = '/viewer/loaddrawing/' + '?key=' + value;
var htm = '\<iframe src="' + uri +'" onload="downloadComplete()">\</iframe>';
document.getElementById('divtitle').innerHTML = htm;