Zclass subclassing Image - how to manage uploads?
I've seen numerous postings about this, but so far I have been unsuccessful in successfully subclassing the Image with my own ZClass (called VProduct for this case). Here's what I have managed to do: * Create my new VProduct ZClass with Image as a base class * Add a couple of new simple (string) properties to VProduct * Modify the VProduct_add and VProduct_addForm methods so that my new properties gets asked for and filled in (and using the system clock to generate unique id's) This seem to work great, and I can move about in the Zope filesystem and add instances of my VProduct class as I want to. I have not been able to successfully integrate the file upload feature. What I have done is: * Add an input file <input type=file name=file> in the addForm method * Putting a file name in this field and pressing submit button
From here I am not quite sure what to do with the image, but from browsing earlier postings about this, it seems to have something to do with calling manage_upload one way or the other.
In the VProduct_add method, I have the following special code: <dtml-call "REQUEST.set('ts', ZopeTime ())"> <dtml-call "REQUEST.set('id', _.str(_.int('ts')))"> The above code generates a unique id for my VProduct instances, and this works fine. Inside the automatically generated: <dtml-with "VProduct.createInObjectManager(REQUEST['id'], REQUEST)"> further down in the VProduct_add method I have the following stuff: <dtml-call "propertysheets.Basic.mange_editProperties (REQUEST)"> that code should take care of modifying the properties of my VProduct instance (and assumes I guess that the input field names match the property names). This seams to work great. Right before the "propertysheets..." line I have tried adding the following: <dtml-call "manage_upload(REQUEST['file'])"> It does not generate an error. But it does not seem to manage the upload part either, since my VProduct instance gets the content_type "text/plain" (instead of image/jpeg which is the right type for the jpeg file I try to upload). And when I try to "view" the VProduct instance, I get a simple <img src="91231912" alt="sometext"> tag which displays like a missing image in a web browser (no wonder since the content_type is wrong). I have tried manually setting the content_type to "image/jpeg", but the image still does not display correctly. My guess is that the image never got uploaded. Any ideas on how to handle the upload properly? I've done some 30 searches through the list archive, without any clear answers to how the image uploads are handled (or should be handled). I've looked carefully through the howto's but have not found anything. Any help would be appreciated. Marius Kjeldahl
Marius Kjeldahl wrote:
Right before the "propertysheets..." line I have tried adding the following:
<dtml-call "manage_upload(REQUEST['file'])">
I *think* you want REQUEST['file'].read(). manage_upload wants a string of data, not a file object. -Michel
Michel Pelletier wrote:
Marius Kjeldahl wrote:
Right before the "propertysheets..." line I have tried adding the following:
<dtml-call "manage_upload(REQUEST['file'])">
I *think* you want REQUEST['file'].read(). manage_upload wants a string of data, not a file object.
Ok, I guess this is where I get confused. When is the REQUEST['file'] instance a string and when is it not? If I try to replace the manage_upload call with your suggestion, I get a message saying: Error Type: AttributeError Error Value: 'string' object has no attribute 'read' which indicates that REQUEST['file'] is a string at this stage (and probably the filename or similar). From the input form I guess "file" is a string (at least that is what it looks like in the input form - the filename) at least until I press the submit button. When it actually changes from the filename to a file I do not know. Any other suggestions on how to get hold of that file? Thanks, Marius Kjeldahl
Marius Kjeldahl wrote:
Ok, I guess this is where I get confused. When is the REQUEST['file'] instance a string and when is it not? If I try to replace the manage_upload call with your suggestion, I get a message saying:
Error Type: AttributeError Error Value: 'string' object has no attribute 'read'
Your form element should be: <input type=file name="file_name:file"> The ':file' is necessary to marshal the file into a file object. I was going on the assumption that you had done that otherwise as you've discovered it will turn it into a string, which you may or may not want. -Michel PS Don't forget the ENCTYPE="multipart/form-data" on your <form> tag.
Michel Pelletier wrote:
Your form element should be:
<input type=file name="file_name:file">
The ':file' is necessary to marshal the file into a file object. I was going on the assumption that you had done that otherwise as you've discovered it will turn it into a string, which you may or may not want.
Ok, I have changed my form element to what you suggested. These are the results I am getting with various <dtml-call> tags in the VProduct_add method (called after filling in VProduct_addForm): * <dtml-call "REQUEST['file'].read ()"> Error Type: TypeError Error Value: argument file was ommitted I guess this is because no field in the form is named 'file'. So I try: * <dtml-call "REQUEST['file_name'].read ()"> ..and I get the same error. I even tried to name="file:file" in the form element, and using REQUEST['file'].read () in the call, but then I get: Error Type: AttributeError Error Value: 'string' object has no attribute 'read' which I guess means that it now has the 'file' variable, but it is still a string. Any other pointers, or references to where I can read more about the file stuff? Thanks in advance, Marius Kjeldahl
Marius Kjeldahl wrote:
Michel Pelletier wrote:
Your form element should be:
<input type=file name="file_name:file">
The ':file' is necessary to marshal the file into a file object. I was going on the assumption that you had done that otherwise as you've discovered it will turn it into a string, which you may or may not want.
Ok, I have changed my form element to what you suggested. These are the results I am getting with various <dtml-call> tags in the VProduct_add method (called after filling in VProduct_addForm):
* <dtml-call "REQUEST['file'].read ()">
Error Type: TypeError Error Value: argument file was ommitted
I guess this is because no field in the form is named 'file'. So I try:
* <dtml-call "REQUEST['file_name'].read ()">
..and I get the same error. I even tried to name="file:file" in the form element, and using REQUEST['file'].read () in the call, but then I get:
Error Type: AttributeError Error Value: 'string' object has no attribute 'read'
which I guess means that it now has the 'file' variable, but it is still a string.
Any other pointers, or references to where I can read more about the file stuff?
REQUEST['file'].read () ^ Notice this space? That space isn't supposed to be there. You're supposed to be calling a function with no arguments, the () has to be right next to the "read". The way you have it right now, it's looking for an attribute called read instead of a function. -- Nick Garcia | ngarcia@codeit.com CodeIt Computing | http://codeit.com
Did you remember the ENCTYPE="multipart/form-data" on your <form> tag? -jfarr ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Hi! I'm a signature virus. Copy me into your .sig to join the fun! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
participants (4)
-
Jonothan Farr -
Marius Kjeldahl -
Michel Pelletier -
Nick Garcia