0

My goal is to download a file from a file-sharing application (Egnyte) and render it in my own HTML. I'm working in Django.

I'm getting a 200 response from the storage service:

response.headers:

{'Content-Type': 'application/pdf;charset=UTF-8',...etc}

response.content: first 100 char. Type <Class Bytes>.

b'%PDF-1.6\r%\xe2\xe3\xcf\xd3\r\n12090 0 obj\r<</Filter/FlateDecode/First 2063/Length 4726/N 197/Type/ObjStm>>stream\r\n'

However, when I pass the response.content to the template.html as the source for an embed, the page is blank.

What am I missing?

I've tried using different html containers. I don't get any errors but in all cases the element is just blank.

<iframe src="b'%PDF-1.6\r%\xe2\xe3\...etc'">
  • The file is definitely PDF, I uploaded a .txt file and, when I request that, it sends back header: 'text/plain;charset=UTF-8' – DaCoPro Apr 06 '23 at 16:10

1 Answers1

0

Ok, a few days later I worked this out using Python, rather than embeding with JS.

    file = download_file(user_profile.token)
    
    with open('file.pdf', 'wb') as pdf:
        pdf.write(file.content)
    
    with open('file.pdf', 'rb') as pdf:
        response = HttpResponse(pdf.read(), content_type="application/pdf")
        response['Content-Disposition'] = 'inline;filename=some_file.pdf'
        return response

Now, when I hit this view, Django serves the file beautifully.