Pregunta

I'm using Zope version 2.8.5 and python 2.3.5 (It's too old I know, but it's for a legacy project and no plans of migrating in the near future)

My code currently looks as under:

def readFiles (file_location):
    self.REQUEST.RESPONSE.setHeader('Content-type','application/octet-stream')
    self.REQUEST.RESPONSE.setHeader('Content-Disposition', 'Content-Disposition: attachment; filename="'+file_name+'"')
    return open(file_location).read()

The above is an External Method and is called by a python script as under -

return context.readFiles(file_location)

All of this works fine for text files, even .doc extension word files. But when I try to download a .docx file using the above code, the files downloads well but it doesn't open.

I get an error that the file is corrupted and can not be opened.

Can anyone suggest how to resolve this?

¿Fue útil?

Solución

Don't worry about it. Found the problem. I had to open the file with mode 'rb'

f= open(file_location, 'rb')
r= f.read()
f.close()
return r
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top