I am trying to allow the user to preview and download ulpoaded documents to the db, but i keep getting
ValueError: read of closed file
when i open from the admin and click on the document it opens in the browser.
views.py
def preview_file(request, file_id):
converted_file = get_object_or_404(OutputFile, id=file_id)
print(f"converted: {converted_file}")
print(f"converted_path: {converted_file.file.path}")
# Assuming the file is stored in 'media/converted' directory
file_path = converted_file.file.path
# Open the file and return it as a response
with open(file_path, 'rb') as file:
response = FileResponse(file)
return response
def download_file(request, file_id):
converted_file = OutputFile.objects.get(id=file_id)
# Assuming the file is stored in 'media/converted' directory
file_path = converted_file.file.path
# Open the file and return it as a response with content-disposition set for download
with open(file_path, 'rb') as file:
response = FileResponse(file)
response['Content-Disposition'] = f'attachment; filename="{converted_file.filename}"'
return response
terminal:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/usr/local/lib/python3.9/wsgiref/handlers.py", line 183, in finish_response
for data in self.result:
File "/usr/local/lib/python3.9/wsgiref/util.py", line 37, in __next__
data = self.filelike.read(self.blksize)
ValueError: read of closed file
[11/Nov/2023 15:37:05] "GET /preview/b68230a0-b209-443f-93d4-dc1e3574c0fc/ HTTP/1.1" 500 59