io.UnsupportedOperation on FileResponse (Django)

dougM

New Member
#1
I have a solution that works in development (VScode, Django v5.1, Python 3.12.5) returning a pdf file created in a buffer using io.BytesIO then return a page response using FileResponse. The following is part of the code that works in the development environment:

buffer = io.BytesIO()
myCanvas = Canvas(buffer, pagesize=letter)
{... more code here to produce the pdf content ... }
myCanvas.showPage()
myCanvas.save()
buffer.seek(0)
return FileResponse(buffer, as_attachment=False, filename="filename.pdf")

Now when I deploy (Ubuntu 22.04 , OpenLiteSpeed 1.7.19, Django 5.1, Python 3.10.12) I get a ‘500 Internal Error’. Looks like all of the generation code works but when returning the resulting buffer in FileResponse() the error occurs. The only error I see in the logs is in the lsws/logs/stderr.log which says ‘io.UnsupportedOperation: fileno’. I'm not sure this is a OpenLiteSpeed issue or a Django issue but any help would be appreciated.
 

Cold-Egg

Administrator
#2
Try Python build-in web server, e.g. python manage.py runserver x.x.x.x:x to start the service, if the issue happens with it, then it's a code issue.
 

dougM

New Member
#3
Thanks for idea ... based on someone else's post the issue is how the response stream is handled by some underlining process. Running under VScode a stream made by io.BytesIO seems to by handled fine. When running under OpenLiteSpeed, there is some process that expects the stream to be a file handle and will throw an exception if it is not. Once I changed my code to create a temp pdf file and pass that it works perfectly. Not sure where the issue is but the 'fix' is easy at least for my application. Thanks again.
 
#4
Hi, I have the exact same problem. Please, would mine explaining? Where do you create and pass the temporary pdf file? Thank you!
 
#5
I have something that works. Thanks for the hint.
myCanvas = Canvas("temp.pdf", pagesize=letter)
...
return FileResponse(open('temp.pdf', 'rb'), as_attachment=False, filename="filename.pdf")
 
Top