-----------------------------------
sample file upload
-----------------------------------
# controller.py
@turbogears.expose()
def upload(self, upload_file, pagename, **keywords):
try:
p = Page.byPagename(pagename)
except SQLObjectNotFound:
turbogears.flash("Must save page first")
turbogears.redirect("/%s" % pagename)
data = upload_file.file.read()
target_file_name = os.path.join(os.getcwd(), UPLOAD_DIR,
upload_file.filename)
try:
u = UploadedFile.byFilename(upload_file.filename)
turbogears.flash(
"File already uploaded: %s is already at %s" % \
(upload_file.filename, target_file_name))
except SQLObjectNotFound:
# open file in binary mode for writing
f = open(target_file_name, 'wb')
f.write(data)
f.close()
turbogears.flash(
"File uploaded successfully: %s saved as: %s" \
% (upload_file.filename, target_file_name))
u = UploadedFile(filename=upload_file.filename,
abspath=target_file_name, size=0)
Page.byPagename(pagename).addUploadedFile(u)
turbogears.redirect("/%s" % pagename)
-------------------------------
On 12月27日, 上午3时43分, Chen Houwu <[email protected]> wrote:
> Oh.........
> I have use dir(doc) here, and got its attributes, now I know how to
> use it!
> But I still think the documentation should be more detail
>
> On 12月27日, 上午3时31分, Chen Houwu <[email protected]> wrote:
>
> > 1. thanks for the reply, for the second question, I have tried your
> > solution, and it works!
>
> > 2. for the first question. Here is my code
> > ------------------------------------------------------
> > class MyController(BaseController)
> > class DocForm(TableForm):
> > class fields(WidgetsList):
> > name = TextField(validator=NotEmpty(strip=True))
> > doc=FileField()
> > description = TextArea(rows=3, cols=25)
>
> > @expose('docman.templates.add_doc')
> > @require(not_anonymous())
> > def add_doc(self, **kw):
> > tmpl_context.form=self.FileForm(action=tg.url
> > ('add_doc_handler') )
> > tmpl_context.dir=DBSession.query(Directory).get(self.dir_id)
> > return
>
> > @validate(form=DocForm(), error_handler=add_doc)
> > @expose()
> > @require(not_anonymous())
> > def add_doc_handler(self, doc, name, description):
> > #*******************************
> > #how can I use the 'doc' parameter? Is there any description?
> > #*********************************
> > -----------------------------------------------------
>
> > I find the following documentation from the formencode website:
>
> > -----------------------------------------------
> > class formencode.validators.FileUploadKeeper(*args, **kw)¶
>
> > Takes two inputs (a dictionary with keys static and upload) and
> > converts them into one value on the Python side (a dictionary with
> > filename and content keys). The upload takes priority over the static
> > value. The filename may be None if it can’t be discovered.
>
> > Handles uploads of both text and cgi.FieldStorage upload values.
>
> > This is basically for use when you have an upload field, and you
> > want to keep the upload around even if the rest of the form submission
> > fails. When converting back to the form submission, there may be extra
> > values 'original_filename' and 'original_content', which may want to
> > use in your form to show the user you still have their content around.
>
> > To use this, make sure you are using variabledecode, then use
> > something like:
>
> > <input type="file" name="myfield.upload">
> > <input type="hidden" name="myfield.static">
>
> > Then in your scheme:
>
> > class MyScheme(Scheme):
> > myfield = FileUploadKeeper()
>
> > Note that big file uploads mean big hidden fields, and lots of
> > bytes passed back and forth in the case of an error.
> > --------------------------------------------------
> > does it mean i can use 'doc.original_filename' and
> > 'doc.original_content' here?
>
> > On 12月27日, 上午2时48分, "Jorge Vargas" <[email protected]> wrote:
>
> > > On Fri, Dec 26, 2008 at 12:39 PM, Chen Houwu <[email protected]> wrote:
>
> > > > Current I am develop some web application with TG2, but I can't solve
> > > > the follown problem:
>
> > > > 1. How to use FileFields?
>
> > > This works exactly was any other field, is your problem on the
> > > controller or the rendering part?
> > > if it's at the controller part you need, a "special" controller to
> > > upload the file, go a search of the group as several implementations
> > > have been posted. Including mine.
>
> > > > 2. How to give a TextField a default value?
>
> > > you pass the value at render time. For example if you have a widget,
> > > named "widget" and it has a "case_id" field the following code will
> > > set it to "
>
> > > ${tmpl_context.widget(dict(case_id=value))}
>
> > > > I can't find any example about them.
> > > > Event from the source code, I can't get suffient clue.
>
> > > > Thanks very much.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---