Note: please post in plain text if possible! 

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

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