0


我正在尝试为trac编写一个插件,但我错过了 sthg。在阅读了网站trac制作的所有教程之后。因此,我尝试使用 POST 方法将文件上传到服务器,这是一个简化的示例:

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

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

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

任何想法或想法?
谢谢

4

2 回答 2

0

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

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

于 2012-09-18T17:40:24.080 回答
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 []

这是本例 GanttTemplate.html 文件中的 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 />

谢谢 !

于 2012-09-19T12:51:03.590 回答