On May 8, 6:49 am, Adrian von Bidder <[email protected]> wrote:
> On Friday 08 May 2009 01.20:43 Derick Eisenhardt wrote:
>
> > I'd assume you would just want to use an upload widget...either from
> > Tosca Widgets or your favorite Javascript library?
>
> I created a form with FileField from tw.  (I've not looked into js libraries
> and all this yet, I'm just trying to get a very basic app.)
>
> As I've said, the template part is not the problem, but I can't easily find
> an example for how to do the controller.

Because I spent ages looking for an example last week (and eventually
found one in japanese, which I had
to translate with google!), here's one that worked for me...


###############################################################################
# EXAMPLE OF CONTROLLER

# all necessary tg imports beforehand
from tw.forms import FileField

class UploadYourFoto(TableForm):

    class fields(WidgetsList):
        my_file = FileField(label_text='Your Foto')
    submit_text = 'Upload Foto!'

upload_foto_form = UploadYourFoto
("upload_foto_form",action='save_file')


class RootController(BaseController):
    @expose('yourapp.templates.upload')
    #...@require(predicates.in_group('members',msg=l_('Only for
members!')))
    def upload(self,**kw):
        tmpl_context.form = edit_profile_form
        return dict(page="Upload")

    @expose() # don't expose this
    def save_file(self,**kw):
        """
        JFYI - there are a few useful properties of the request.POST
['your_file'] which you could use
        - things like:
        request.POST['your_file'].type : a string with something like
'image/jpeg'
        request.POST['your_file'].size : the filesize of e.g. yiur
uploaded image
        """
        target = "/tmp/my_uploaded_file.jpg"
        if request.POST.has_key('my_file'):
            if request.POST['your_file'].file:
                try:
                    fd = open(target,'w')
                    fd.write(request.POST['your_file'].file.read())
                    fd.close()
                except Exception,e:
                    flash("Horrible exception: %s"%str(e))
                else:
                    flash("It worked! File saved to %s"%target)
                redirect('/upload') # back to upload to upload
another?

#################################################################

#################################################################
# EXAMPLE OF TEMPLATE

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml";
      xmlns:py="http://genshi.edgewall.org/";
      xmlns:xi="http://www.w3.org/2001/XInclude";>

  <xi:include href="master.html" />

<head>
  <meta content="text/html; charset=UTF-8" http-equiv="content-type"
py:replace="''"/>
  <title>Upload a foto?</title>
</head>
<body>
    <div py:replace="tmpl_context.form()">Upload Foto (this text
doesn't get shown)</div>
</body>
</html>

##############################################################################
>
> Ideally, I could get information from the request before the whole thing is
> loaded into memory (my example/playground app is a image gallery, so when I
> upload 20 pictures via a upload form, or even just a 100M TIFF because I'm
> crazy, there is no excuse for the web server to grab this much RAM.  Ever.  
> (Imagine 50 users doing this at he same time ...)
>
> (... yes, I'm kind of old fashioned ... last time I played with web
> technologies, I wrote it as a Perl cgi program and parsed the requests for
> myself ...)
>
> -- vbi
>
>
>
> > On May 7, 11:59 am, Adrian von Bidder <[email protected]> wrote:
> > > On Sunday 05 April 2009 21.42:32 Daniel Fetchinson wrote:
> > > > > Thanks in advance
>
> > > >http://lmgtfy.com/?q=turbogears+file+upload
>
> > > You know, learning tg while commuting by train (this means not beng
> > > online) is a PITA sometimes. :-)
>
> > > [[ I'm online now.  Well, lmgtfy.  How funny. ]]
>
> > > #1: right now, it's difficult for a beginner to sort tg1 and tg2 in
> > > Google's search results and judge what is still relevant in the latter.
>
> > > #2: this shouldn't be a case for google.  File uploads are fundamental.
> > >  Is there work to convert the file upload tutorial from 1.0 to 2.0?
>
> > > Since I'm just learning both python and tg, it's kind of hard for me to
> > > build the proper stuff.  So: pointers for tg2 very welcome.  Quick and
> > > dirty for now is ok, since I'm just fooling around, but as I want to
> > > learn this properly, file uploads for me immediately beg the question:
> > > how to do them without having to load the full file into memory?
>
> > > cheers
> > > -- vbi
>
> > >  signature.asc
> > > < 1KViewDownload
>
> > >
>
>
>  signature.asc
> < 1KViewDownload

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to