quick search:
 

Upload Multiple Files into a Directory

Submitted by: runyaga
Last Edited: 2004-10-14

Category: Python(Script)

Average rating is: 3.0 out of 5 (2 ratings)

Description:
often times people want to upload multiple files. well, here a way of doing that. this Script(Python) assumes a naming convention in forms: file.XXXX.id for the id of the file object and file.XXXX for the uploaded file. If you wanted to upload multiple files, this is the program for you.

Source (Text):
#create a Script (Python) called upload_files
REQUEST=context.REQUEST
sentFiles = {}

for key in context.REQUEST.keys():             # get all the ids
    if key[:5]=='file.' and key[-3:]=='.id':   # we find id file.XXX.id
            sentFiles[REQUEST[key]]=''

for key in context.REQUEST.keys():
    if key[:5]=='file.' and key[-3:]!='.id':    #we've got a file
        #found its id and make sure file exists
        if sentFiles.has_key(REQUEST[key+'.id']) and REQUEST[key].filename:
            sentFiles[REQUEST[key+'.id']]=REQUEST[key]
        else:
            del sentFiles[REQUEST[key+'.id']]    #remove keys that dont have files

#now that we have created a dictionary w/ a id and we know its value is a uploaded file
#lets write them to the context's folder

for k in sentFiles.keys():
    context.manage_addFile(k, sentFiles[k])

return 'Finished adding ' + str(len(sentFiles)) + ' files'

#create a DTML Method called uploadForm
<dtml-var standard_html_header>
<h2><dtml-var title_or_id> <dtml-var document_title></h2>
<p>


<form method="post" action="upload_files"
      enctype="multipart/form-data">

File 1 - id:       <input type=text name="file.name1.id"> <br>
File 1 - contents: <input type=file name="file.name1">  <br>

File 2 - id:       <input type=text name="file.name2.id"> <br>
File 2 - contents: <input type=file name="file.name2"> <br>

<input type=submit method=submit>
</form>


</p>
<dtml-var standard_html_footer>

Explanation:
this loops through REQUEST.keys to find the convention file.XXXX.id to
pick out all of the ids used in the upload form. we add them to a dictionary,
sentFiles. Now we loop over the keys picking out the file objects and seeing if
a key already exists, if it does already exist AND a file has been uploaded then we
add that object to the dictionary, otherwise we delete the key from the dictionary,
because files that arent uploaded dont need to be added.

then we loop through the dictionary contents to create all of our File objects.

you could easily take the ID requiste out of this program by just checking to see if the
obj.filename attribute exists. if it does exist you could just use that as the id
for the new object.

for a simplified version of what this recipe is doing please see
"http://www.zopelabs.com/cookbook/1006887320":Upload File or Image recipe


Comments:

a different directory by Papercup - 2004-10-14
what if I want to upload the file to a diferent directory

for example upload to:

directory/subdirectory
 
Re: a different directory by runyaga - 2004-10-14
for k in sentFiles.keys():
    #context.manage_addFile(k, sentFiles[k])
    context.directory.subdirectory.manage_addFile(k,sentFiles[k])

NOTE: if your directories have punctuation you may have to
'progrmatically traverse' to the folder.  see getatttr() recipe.
i.e.

sub-folder/sub.subfolder would be:
    destination=getattr(context,'sub-folder')
    destination=getattr(context,'sub.subfolder')
    destination.manage_addFile(k, sentFiles[k])

or try this:
    destination=context.restrictedTraverse('sub-folder/sub.subfolder')
    destination.manage_addFile(k, sentFiles[k])

#Should this be made into a seperate recipe? Traversing the ZODB?


 
Re: Re: a different directory by comrade - 2004-10-14
I think it should be a recipe... it looks *very* useful and I expect some people will slip up using filenames with extensions. 


upload file to different directory depending on key by b_farley - 2004-10-14


upload file to different directory depending on key by b_farley - 2004-10-14
Hello--I am trying to use this recipe and am not having any luck with the file going to the proper directory from the id key.  Can you please take a look at upload_files script modifications and advise me what I am doing wrong? --enclosing the modifications I made which are just point to the individual folders I made.

for k in sentFiles.keys():
     #context.manage_addFile(k, sentFiles[k])
        context.lecture.manage_addFile(k,sentFiles[k])
        context.quizzes.manage_addFile(k,sentFiles[k])
        context.exams.manage_addFile(k,sentFiles[k])

Thank you, Becki.