Zopistas, I'm trying to store files that are submitted from a form on the local filesystem, and not in the ZODB. First, I thought I could use the LocalFS product, but this is read only, so no dice. Then I tried to implement the things I found in the "Howto store files externally"-howto. I'm almost there (after some fiddling around, the howto is a bit lacking) but I get errors. When I try to add an object of the class "myFileClass", I get the following: "Zope has encountered an error while publishing this resource. Error Type: NameError Error Value: cookId" The external method "cookId" is located in the ZClass. Traceback below. Shouldn't it be easy to store files outside the ZODB? I find these methods a bit cumbersome. Alexander Limi. http://mp3.no Traceback (innermost last): File D:\Programs\Zope\lib\python\ZPublisher\Publish.py, line 214, in publish_module File D:\Programs\Zope\lib\python\ZPublisher\Publish.py, line 179, in publish File D:\Programs\Zope\lib\python\Zope\__init__.py, line 202, in zpublisher_exception_hook (Object: RoleManager) File D:\Programs\Zope\lib\python\ZPublisher\Publish.py, line 165, in publish File D:\Programs\Zope\lib\python\ZPublisher\mapply.py, line 160, in mapply (Object: myFileClass_add) File D:\Programs\Zope\lib\python\ZPublisher\Publish.py, line 102, in call_object (Object: myFileClass_add) File D:\Programs\Zope\lib\python\OFS\DTMLMethod.py, line 145, in __call__ (Object: myFileClass_add) File D:\Programs\Zope\lib\python\DocumentTemplate\DT_String.py, line 502, in __call__ (Object: myFileClass_add) File D:\Programs\Zope\lib\python\DocumentTemplate\DT_With.py, line 133, in render (Object: myFileClass.createInObjectManager(cookId(REQUEST['file']), REQUEST)) File D:\Programs\Zope\lib\python\DocumentTemplate\DT_Util.py, line 335, in eval (Object: myFileClass.createInObjectManager(cookId(REQUEST['file']), REQUEST)) (Info: REQUEST) File <string>, line 0, in ? NameError: (see above)
Alexander Limi wrote:
Zopistas,
I'm trying to store files that are submitted from a form on the local filesystem, and not in the ZODB. First, I thought I could use the LocalFS product, but this is read only, so no dice.
Then I tried to implement the things I found in the "Howto store files externally"-howto. I'm almost there (after some fiddling around, the howto is a bit lacking) but I get errors.
Shouldn't it be easy to store files outside the ZODB? I find these methods a bit cumbersome.
Alexander Limi. http://mp3.no
Hi Alex, I just use a simple external method for this purpose. I have a form that has a file input box called "file". The form posts to this external method using method="post" and enctype="multipart/form-data". It's a small function, so I'll just post the code here: def processFile(self, file, REQUEST=None, RESPONSE=None): buffer = file.readlines() outfile = open('%s/new_data.txt' % ATTACHMENTS_HOME, 'w') outfile.writelines(buffer) if REQUEST is not None: return RESPONSE.redirect('%s/updated.html' % REQUEST['URL1']) ATTACHMENTS_HOME is a variable I define elsewhere in the module. After the file is saved, I just have it redirect to a page called updated.html. There's normally some processing that goes on between the readlines() and the writelines(), but I cut all that out so you could see the basics. -- Nick Garcia | ngarcia@codeit.com CodeIt Computing | http://codeit.com
Hi Alex, I just use a simple external method for this purpose. I have a form that has a file input box called "file". The form posts to this external method using method="post" and enctype="multipart/form-data".
It's a small function, so I'll just post the code here:
def processFile(self, file, REQUEST=None, RESPONSE=None):
buffer = file.readlines()
outfile = open('%s/new_data.txt' % ATTACHMENTS_HOME, 'w') outfile.writelines(buffer)
if REQUEST is not None: return RESPONSE.redirect('%s/updated.html' % REQUEST['URL1'])
ATTACHMENTS_HOME is a variable I define elsewhere in the module. After the file is saved, I just have it redirect to a page called updated.html.
This _almost_ does the trick. Text files work fine, but GIFs do not. I suspect it may be because the buffer processing adds something to the data - in any case the file is garbled in the other end. The size of the file is not correct, in one particular case the original file was 5627 bytes, the processed file was 5654 bytes, ie. it ends up being 27 bytes larger than the original. Any suggestions? The enc-type is multipart/form-data. Alexander Limi http://mp3.no
* Alexander Limi (alexander@limi.net) [991218 01:22]:
Hi Alex, I just use a simple external method for this purpose. I have a form that has a file input box called "file". The form posts to this external method using method="post" and enctype="multipart/form-data".
It's a small function, so I'll just post the code here:
def processFile(self, file, REQUEST=None, RESPONSE=None):
buffer = file.readlines()
outfile = open('%s/new_data.txt' % ATTACHMENTS_HOME, 'w') /snippage/
This _almost_ does the trick. Text files work fine, but GIFs do not. I suspect it may be because the buffer processing adds something to the data - in any case the file is garbled in the other end. The size of the file is not correct, [..] , ie. it ends up being 27 bytes larger than the original.
Tried treating the file as binary? outfile = open('%s/new_data.txt' % ATTACHMENTS_HOME, 'wb') ^ tal.
Alexander Limi wrote:
def processFile(self, file, REQUEST=None, RESPONSE=None):
buffer = file.readlines()
outfile = open('%s/new_data.txt' % ATTACHMENTS_HOME, 'w') outfile.writelines(buffer)
if REQUEST is not None: return RESPONSE.redirect('%s/updated.html' % REQUEST['URL1'])
ATTACHMENTS_HOME is a variable I define elsewhere in the module. After the file is saved, I just have it redirect to a page called updated.html.
This _almost_ does the trick. Text files work fine, but GIFs do not. I suspect it may be because the buffer processing adds something to the data - in any case the file is garbled in the other end. The size of the file is not correct, in one particular case the original file was 5627 bytes, the processed file was 5654 bytes, ie. it ends up being 27 bytes larger than the original.
Any suggestions? The enc-type is multipart/form-data.
Change this line: outfile = open('%s/new_data.txt' % ATTACHMENTS_HOME, 'wb') This will open the file for writing binary data. Should work fine for text too. -- Nick Garcia | ngarcia@codeit.com CodeIt Computing | http://codeit.com
Alexander Limi wrote:
Hi Alex, I just use a simple external method for this purpose. I have a form that has a file input box called "file". The form posts to this external method using method="post" and enctype="multipart/form-data".
It's a small function, so I'll just post the code here:
def processFile(self, file, REQUEST=None, RESPONSE=None):
buffer = file.readlines()
outfile = open('%s/new_data.txt' % ATTACHMENTS_HOME, 'w') outfile.writelines(buffer)
if REQUEST is not None: return RESPONSE.redirect('%s/updated.html' % REQUEST['URL1'])
ATTACHMENTS_HOME is a variable I define elsewhere in the module. After the file is saved, I just have it redirect to a page called updated.html.
This _almost_ does the trick. Text files work fine, but GIFs do not. I suspect it may be because the buffer processing adds something to the data - in any case the file is garbled in the other end. The size of the file is not correct, in one particular case the original file was 5627 bytes, the processed file was 5654 bytes, ie. it ends up being 27 bytes larger than the original.
Any suggestions? The enc-type is multipart/form-data.
If the file is GIF, then you shouldn't use readlines(), because there are no lines. And as the other postings suggested, the output file should be opened with the 'wb' flag. Note a problem might arise in writing to a external file: Be sure to avoid conflict. If two persons are submitting the same form and the external method want to write to the same file, there will be a problem.
Alexander Limi http://mp3.no
_______________________________________________ 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 )
Alexander Limi wrote:
Shouldn't it be easy to store files outside the ZODB? I find these methods a bit cumbersome.
I've been able to write to FS files using External Methods. There is a sampling of my code. ================================ import os, re, string, time from ZPublisher import Client def indexThis(self, idxkey=None): """ accept idxkey and write the indexable version to FS """ if not idxkey: return 'No idxkey' REQUEST = self.REQUEST fname = "/home/sites/" + idxkey path = os.path.dirname(fname) if not os.path.isdir(path): os.makedirs(path) url = REQUEST.BASE0 + "/" + idxkey + "/page_clean" newString = Client.call(url,None,None)[1] output = open(fname,'w') output.write(newString) output.close() atime = time.mktime(time.strptime(string.split(fname,'/')[5],'%Y-%m-%d')) os.utime(fname,(atime,atime)) return url ===================================== Above the idxkey is basically the URL path of the object which becomes the path for the FS file. When indexThis is called it actually makes an exterenal web request, via Client.call(), and stores the results to the filesystem. I was not able to get the method page_clean to run on the proper object via other means. The messing around with the timestamp of the file is used to make sure our index engine indexes in the proper order. Python makes writting out to the FS incredibly easy. Hope this helps. ------------------------------- tonyr@ep.newtimes.com Director of Web Technology Newtimes Inc. -------------------------------
participants (5)
-
Alexander Limi -
Kaleissin -
Li Dongfeng -
Nick Garcia -
tonyr@ep.newtimes.com