Hello, I wonder if there is an elegant method to check a file which is intended to be stored as (Ext)File in the ZODB according to the validity of its contents. The reason is thatr I have to store some PDF files but I have users who do not really know the difference between DOC and PDF. I want to refuse DOCs (or any other non-PDF) to enforce users to upload just PDF. Kind regards ANdreas.
On Tue, Oct 29, 2002 at 10:32:53PM +0100, Andreas Tille wrote:
I wonder if there is an elegant method to check a file which is intended to be stored as (Ext)File in the ZODB according to the validity of its contents. The reason is thatr I have to store some PDF files but I have users who do not really know the difference between DOC and PDF. I want to refuse DOCs (or any other non-PDF) to enforce users to upload just PDF.
check the content_type of the object if possible. application/pdf is for ... PDF IIRC bye, Jerome Alet
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.
Hi Andreas, the most simple approach is to limit this a bit in the HTML Form: <input type="file" name="Name" accept="application/pdf"> This would only show pdf-documents in the file requester. Then again you can use the file-object in the form and validate it before passing to manage_addFile() Regards Tino --On Dienstag, 29. Oktober 2002 22:32 +0100 Andreas Tille <tillea@rki.de> wrote:
Hello,
I wonder if there is an elegant method to check a file which is intended to be stored as (Ext)File in the ZODB according to the validity of its contents. The reason is thatr I have to store some PDF files but I have users who do not really know the difference between DOC and PDF. I want to refuse DOCs (or any other non-PDF) to enforce users to upload just PDF.
Kind regards
ANdreas.
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
On Tue, 29 Oct 2002, Tino Wildenhain wrote:
<input type="file" name="Name" accept="application/pdf"> I do so, but I did not found any browser who respects this ...
Then again you can use the file-object in the form and validate it before passing to manage_addFile() In fact this is the problem which I explained in detail in my other mail.
Kind regards Andreas.
participants (3)
-
Andreas Tille -
Jerome Alet -
Tino Wildenhain