Note: please post in plain text if possible! and do not delete the whole 
context in replies as it makes it harder to get the whole picture...

> -----Original Message-----
> From: [email protected] On Behalf Of cyberbrain
> Sent: 19 September 2012 09:15
> To: [email protected]
> Subject: [Trac] Upload a file to process it ! and than save 
> it with some modifications !
> 
> I'm trying to write a plugin for trac 
> <http://trac.edgewall.org/>  but I'm missing sthg.

`sthg`?  What does that mean?

> I'm trying to upload a file to the sever using the POST 
> method this a simplified example :
> 
> <form id="MyForm" name="input" action="" method="post">
>     <label for="attachment">URL :</label>           
>     <input type="file" name="GanttFile" value=""/>
> </form>
> 
> Now I'm trying to process the uploaded file ,read it and do 
> some modifications than save it or ask the user to choose 
> where he wants to save the file (export some data from the 
> trac database)...I'm still blocked at this level :
> 
> def process_request(self, req):
>     data = {}
>     if req.method=='POST':
>         file=req.args.get('GanttFile', 'value')
>         # and now I'm blocked !! how can I modify this file 
>         # and then redirect or save it !   

You cannot just get the file contents that easily.  Here is the skeleton of 
what I came up with after searching the web for examples:

    # ----------------------------------------------------------------------- #
    def _do_upload(self, req):
        """_do_upload(self, req)
        
        Try to read and process data from the uploaded request.
        Will redirect either to the details page...
        ...or the main list with an error message.
        """
        gantt_file = req.args.get('GanttFile')

        if not hasattr(gantt_file, 'filename') or not gantt_file.filename:
            add_warning(req, "No License Request file specified.")
            req.redirect(req.href.mainpage())

        if hasattr(gantt_file.file, 'fileno'):
            size = os.fstat(gantt_file.file.fileno())[6]
        else:
            gantt_file.file.seek(0, 2) # seek to end of file
            size = gantt_file.file.tell()
            gantt_file.file.seek(0)
        if size == 0:
            add_warning(req, "Request is empty?")
            req.redirect(req.href.mainpage())

        gantt_data = gantt_file.file.read()
        <processing goes here>

Note: I run this on windows.

> and if I try to display the content of the variable file I 
> just get the name of the file not all the path ? By doing 
> something like this :
> 
> <input type="text" name="file" value ="$myfile" /> 
> 
> and in my source code :
> 
> def process_request(self, req):
>     data = {}
>     if req.method=='POST':
>         myfile=req.args.get('GanttFile', 'value')
>         # display the content 
>         data.update({
>             'myfile': myfile
>         })
>  
> This will display only the file name ...I need the absolute 
> path to do some process !
> 
> I'm I missing sthg. ? is that the right way ?
> 
> Thanks !

I hope that helps...

~ mark c

> -----Original Message-----
> From: cyberbrain [mailto:[email protected]] 
> Sent: 19 September 2012 10:38
> To: [email protected]
> Cc: Cooke, Mark
> Subject: Re: [Trac] Upload a file to process it ! and than 
> save it with some modifications !
> 
> Thanks for your answer it helped a lot ...!
>
> I've tried your suggestion but it seems that i have a problem 
> with this comand
>             gantt_file = req.args.get('GanttFile') 
> It has no attribute called filename ??? so the code get 
> stopped at this level :
> 
>             if not hasattr(gantt_file, 'filename') or not 
> gantt_file.filename:
> 
> any idea ?

What does your html template look like?  This is the relevant bits of mine:-

        <!--! NB: do not use `action="upload"` here as this forms a URL.. -->
        <form class="addnew" id="addcomponent"
              method="post" enctype="multipart/form-data">
          <fieldset>
            <legend>New Request:</legend>
            <div class="field">
              <label>Request File: <br />
                <input type="file" name="GanttFile" size="70" />
              </label>
            </div>
            <div class="buttons">
              <input type="submit" name="upload" value="${_('Upload')}"/>
            </div>
          </fieldset>
        </form>

Note the `enctype` for the form...

~ mark c

-- 
You received this message because you are subscribed to the Google Groups "Trac 
Users" 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/trac-users?hl=en.

Reply via email to