[Zope] Re: Check uploaded file for valid contents
Andreas Tille
tillea@rki.de
Wed, 30 Oct 2002 08:17:13 +0100 (CET)
On Tue, 29 Oct 2002, Jerome Alet wrote:
> check the content_type of the object if possible.
>
> application/pdf is for ... PDF
Well, this is what I wanted to do but how to specify this.
I have a form
<form name="Report" action="addReport" method="post" enctype="multipart/form-data">
<input type="file" name="file" accept="application/pdf"><br />
<input type="submit" value="New PDF file">
</form>
and this is evaluated by the following Python Script addReport :
response = container.REQUEST.RESPONSE
form = container.REQUEST.form
print form
print form['file']
# print form['file'].content_type
container.manage_addProduct['ExtFile'].manage_addExtFile('iid', 'titel', 'descr', form['file'])
return printed
This returns:
{'file': <ZPublisher.HTTPRequest.FileUpload instance at 0x9e658cc>}
<ZPublisher.HTTPRequest.FileUpload instance at 0x9e658cc>
If I uncomment the line in the script to get the content_type I get:
Error Type: AttributeError
Error Value: FileUpload instance has no attribute 'content_type'
If I search in the Zope source code for the class FileUpload I find the
available methods
methods= ['close', 'fileno', 'flush', 'isatty',
'read', 'readline', 'readlines', 'seek',
'tell', 'truncate', 'write', 'writelines']
So I could use 'read' or 'readline' to parse the first bytes whether they
are valid PDF header, but I wonder if there is no more high level method
available. If you think this information might be stored in the headers
attribute - forget it, it just consists of
Content-Type: application/octet-stream
which is true for any uploaded file.
Kind regards
Andreas.