读取和处理Trac插件的上传文件

时间:2012-09-17 14:48:32

标签: plugins file-upload http-post trac


我正在尝试为trac编写一个插件,但我很遗憾。阅读了网站trac制作的所有教程后。 所以我试图使用POST方法将文件上传到服务器,这是一个简单的例子:

<form id="MyForm" name="input" action="" method="post">
<label for="attachment">URL :</label>           
<input type="file" name="GanttFile" value=""/>
</form>

现在我正在尝试处理上传的文件,阅读它并做一些修改而不是保存它或要求用户选择他想要保存文件的位置(从trac数据库导出一些数据)......我m仍然被阻止在这个级别:

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 !    

如果我尝试显示变量文件的内容,我只是得到文件的名称而不是所有路径? 做这样的事情:

<input type="text" name="file" value ="$myfile" /> 

在我的源代码中:

def process_request(self, req):
    data = {}
    if req.method=='POST':
        file=req.args.get('GanttFile', 'value')
        # display the content 
        data.update({
            'myfile': file
        })

任何想法或想法?
感谢

2 个答案:

答案 0 :(得分:0)

添加file = os.path.basename(file)会将路径修剪为文件名,这会导致IE版本的行为类似于Firefox版本(Firefox版本应该不受影响)。

有关您尝试执行此类操作的示例,请参阅Trac的web_ui.py。具体来说,请查看PluginAdminPanel类“_do_install方法的代码。这是通过管理员的Web UI上传.egg文件来安装新插件时使用的代码。

答案 1 :(得分:0)

我有一个解决方案:
Python类:

class ProjectPlugin(Component):
implements(INavigationContributor, IRequestHandler, ITemplateProvider)

# INavigationContributor methods
def get_active_navigation_item(self, req):
    return 'helloworldv2linkIdentifier'

def get_navigation_items(self, req):
    yield ('mainnav', 'helloworldv2linkIdentifier',
           tag.a('Gantt Export', href=req.href.myapppp()))


# IRequestHandler methods
def match_request(self, req):
    return re.match(r'/myapppp(?:_trac)?(?:/.*)?$', req.path_info)

def process_request(self, req):
# add the implements and chek the imports ! and the indents

    data = {}
    if req.method=='POST':
        if 'DispFile' in req.args:
            myFile=req.args.get('Fily','value')
            data.update({
                'myFile': myFile
            })
            dummy=req.args.get('Fily','value').filename
            data.update({
                    'dummy': dummy
                })
            # file  reading
            mystream = myFile.file.read()


    # This tuple is for Genshi (template_name, data, content_type)
    # Without data the trac layout will not appear.
    return 'GanttTemplate.html', data, None

# ITemplateProvider methods
# Used to add the plugin's templates and htdocs
def get_templates_dirs(self):
    from pkg_resources import resource_filename
    return [resource_filename(__name__, 'templates')]

def get_htdocs_dirs(self):
    return []

这是这个案例中的HTML代码GanttTemplate.html文件:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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="layout.html" />

<head>
 <title>Gant Export Tool</title>
</head>

<body> 


<form id="GanttForm1" name="inputForm2" action="" method="POST"     enctype="multipart/form-data">       
    <fieldset id="operations" >
        <legend >
            Configuration
        </legend>

        <label for="Fily">URL :</label>           
        <input type="file" name="Fily" value=""/><br /><br />
        Dummy variable = $dummy            <br/>
        <input type="submit" name="DispFile" value="Display the file"/><br /><br />
    </fieldset>
</form>
<br /><br /><br /><br /><br />

谢谢!