Hi everyone,
I am stuck with an issue - please refer to the code below.
1. I have a form (new_entry function) that I will like to use to be able
to upload a file among other fields.
2. I am using FORM instead of SQLFORM because one of the dropdown fields
(line 11) in the form is generated at run time.
3. The way that I did was to use input file type (line 9) to open a file
selection browser / window to select the file - this works OK.
4. If form.vars.uploadfile is not empty, it means that a file had been
selected for uploading.
5. I will the want to copy the file into application/<app name>/upload
folder using shutils (lines 17-25) but the file name will be changed to
entry.file.<random number>.<original file name>
6. However, this does not work as I get the error that the file is not
found.
7. For debugging, I print the source file name (line 21) and it only gave
me the file name when I will need the entire file path for shutil.copy2()
at line 25 to work.
*Can someone assist?*
Any help will be greatly appreciated.
Thank in advance and warm regards
Maurice Ling
1. def new_entry():
2. if session.username == None: redirect(request.env.http_referer +
"account/log_in")
3. # get unarchived notebooks
4. else:
5. notebook = [notebook['name'] for notebook in
cydb(cydb.notebook.archived == False).select(cydb.notebook.name)]
6. notebook.sort()
7. form = FORM(TABLE(
8. TR('Title: ', INPUT(_type='text', _name='title',
_size=80)),
9. TR('File: ', INPUT(_type='file', _name='uploadfile')),
10. TR('tags: ', INPUT(_type='text', _name='tags',
_size=80)),
11. TR('Notebook: ', SELECT(notebook, _name='notebook')),
12. TR('Description: ', TEXTAREA(_type='text',
_name='description'),
13. TR('',INPUT(_type='submit', _name='SUBMIT')))))
14. if form.accepts(request.vars, session):
15. notebook = cydb(cydb.notebook.name ==
form.vars.notebook).select(cydb.notebook.id).as_list()[0]['id']
16. if form.vars.uploadfile != '':
17. # upload file into applications/<app name>/upload
folder
18. import os, random, shutil
19. upload_dir = os.sep.join([os.getcwd(),
'applications', request.application, 'uploads'])
20. sourcefile = form.vars.uploadfile.filename
21. print sourcefile
22. newfile = upload_dir + os.sep + 'entry.file.' + \
23. str(int(random.random()*10000000000000)) + \
24. os.path.splitext(sourcefile)[-1]
25. shutil.copy2(sourcefile, newfile)
26. cydb.ent.insert(title=form.vars.title,
27. author=session.username,
28. notebook=notebook_id,
29. tags=form.vars.tags,
30. file=newfile.split(os.sep)[-1],
31. filename=newfile.split(os.sep)[-1],
32. description=form.vars.description)
33. else:
34. # no file to upload
35. cydb.ent.insert(title=form.vars.title,
36. author=session.username,
37. notebook=id,
38. tags=form.vars.tags,
39. file='',
40. filename='',
41. description=form.vars.description)
42. bb.tape.insert(event='New entry created. %s. Title =
%s'% \
43.
(cydb(cydb.notebook.id==notebook_id).select(cydb.notebook.name),
44. form.vars.title),
45. user=session.username)
46. response.flash='your entry has been successfully added'
47. return dict(form=form)
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.