如何用python接收html页面上传的文件?

中箭的冒险家 2017-07-10 03:30:13
本人刚接触python,
现在写了一个简单的上传文件的页面效果如下
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>定义input type="file" 的样式</title>
<style type="text/css">
body{
font-size:14px;
align-text:center;
}
input{
vertical-align:middle;
margin:0;
padding:0
}
.file-box{
position:relative;
width:340px;
margin:0px auto;
}
.txt{
height:22px;
border:1px solid #cdcdcd;
width:180px;
}
.btn{
background-color:#FFF;
border:1px solid #CDCDCD;
height:24px;
width:70px;
}
.file{
position:absolute;
top:0;
right:80px;
height:24px;
filter:alpha(opacity:0);
opacity: 0;
width:260px
}
</style>
</head>
<body>
<div class="file-box">
<form action="" method="post" enctype="multipart/form-data">
<input type='text' name='textfield' id='textfield' class='txt' />
<input type='button' class='btn' value='浏览...' />
<input type="file" name="fileField" class="file" id="fileField" size="28" onchange="document.getElementById('textfield').value=this.value" />
<input type="submit" name="submit" class="btn" value="上传" onclick=""/>
</form>
</div>
</body>
</html>
请问如何写python代码,保存上传的文件到指定路径,并返回路径名。
...全文
4340 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
NookVoice 2019-07-11
  • 打赏
  • 举报
回复
引用 1 楼 混沌鳄鱼 的回复:
我用bottle 这个微型框架简单写了一个。 理论说应该没有比这个再轻量级的方案了。


#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
from bottle import *


HTML = """
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>定义input type="file" 的样式</title>
<style type="text/css">
body{
font-size:14px;
align-text:center;
}
input{ 
vertical-align:middle;
margin:0;
padding:0
}
.file-box{
position:relative;
width:340px;
margin:0px auto;
}
.txt{
height:22px;
border:1px solid #cdcdcd;
width:180px;
}
.btn{
background-color:#FFF;
border:1px solid #CDCDCD;
height:24px;
width:70px;
}
.file{
position:absolute;
top:0;
right:80px;
height:24px;
filter:alpha(opacity:0);
opacity: 0;
width:260px
}
</style>
</head>
<body>
<div class="file-box">
<form action="/upload" method="post" enctype="multipart/form-data">
<input type='text' name='textfield' id='textfield' class='txt' />  
<input type='button' class='btn' value='浏览...' />
<input type="file" name="fileField" class="file" id="fileField" size="28" onchange="document.getElementById('textfield').value=this.value" />
<input type="submit" name="submit" class="btn" value="上传" onclick=""/>
</form>
</div>
</body>
</html>
"""


base_path = os.path.dirname(os.path.realpath(__file__))  # 获取脚本路径

upload_path = os.path.join(base_path, 'upload')   # 上传文件目录
if not os.path.exists(upload_path):
    os.makedirs(upload_path)


@route('/', method='GET')
@route('/upload', method='GET')
@route('/index.html', method='GET')
@route('/upload.html', method='GET')
def index():
    """显示上传页"""
    return HTML


@route('/upload', method='POST')
def do_upload():
    """处理上传文件"""
    filedata = request.files.get('fileField')
    
    if filedata.file:
        file_name = os.path.join(upload_path, filedata.filename)
        try:
            filedata.save(file_name)  # 上传文件写入
        except IOError:
            return '上传文件失败'
        return '上传文件成功, 文件名: {}'.format(file_name)
    else:
        return '上传文件失败'

@route('/favicon.ico', method='GET')
def server_static():
    """处理网站图标文件, 找个图标文件放在脚本目录里"""
    return static_file('favicon.ico', root=base_path)


@error(404)
def error404(error):
    """处理错误信息"""
    return '404 发生页面错误, 未找到内容'

run(port=8080, reloader=False)  # reloader设置为True可以在更新代码时自动重载

使用python 2.7 在DOS窗口 pip install bottle 或者下载一个bottle.py放在脚本同一个目录。 https://pypi.python.org/pypi/bottle/0.12.8
确实够简洁,收藏了。 今天正好有这个需求。
CDSoftwareWj 2017-07-11
  • 打赏
  • 举报
回复
这问题问的,这东西要看你用什么框架,还是要从底层自己实现了! bottle,可是说是最小的解决方案了,还有 web2py flask django.......多了去了 你用那个??? 你决定了用那个,剩下的google这框架的教程文档,文件上传是必讲的,会了没?
中箭的冒险家 2017-07-11
  • 打赏
  • 举报
回复
我用的是python3.5.3
混沌鳄鱼 2017-07-10
  • 打赏
  • 举报
回复 2
我用bottle 这个微型框架简单写了一个。 理论说应该没有比这个再轻量级的方案了。


#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
from bottle import *


HTML = """
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>定义input type="file" 的样式</title>
<style type="text/css">
body{
font-size:14px;
align-text:center;
}
input{ 
vertical-align:middle;
margin:0;
padding:0
}
.file-box{
position:relative;
width:340px;
margin:0px auto;
}
.txt{
height:22px;
border:1px solid #cdcdcd;
width:180px;
}
.btn{
background-color:#FFF;
border:1px solid #CDCDCD;
height:24px;
width:70px;
}
.file{
position:absolute;
top:0;
right:80px;
height:24px;
filter:alpha(opacity:0);
opacity: 0;
width:260px
}
</style>
</head>
<body>
<div class="file-box">
<form action="/upload" method="post" enctype="multipart/form-data">
<input type='text' name='textfield' id='textfield' class='txt' />  
<input type='button' class='btn' value='浏览...' />
<input type="file" name="fileField" class="file" id="fileField" size="28" onchange="document.getElementById('textfield').value=this.value" />
<input type="submit" name="submit" class="btn" value="上传" onclick=""/>
</form>
</div>
</body>
</html>
"""


base_path = os.path.dirname(os.path.realpath(__file__))  # 获取脚本路径

upload_path = os.path.join(base_path, 'upload')   # 上传文件目录
if not os.path.exists(upload_path):
    os.makedirs(upload_path)


@route('/', method='GET')
@route('/upload', method='GET')
@route('/index.html', method='GET')
@route('/upload.html', method='GET')
def index():
    """显示上传页"""
    return HTML


@route('/upload', method='POST')
def do_upload():
    """处理上传文件"""
    filedata = request.files.get('fileField')
    
    if filedata.file:
        file_name = os.path.join(upload_path, filedata.filename)
        try:
            filedata.save(file_name)  # 上传文件写入
        except IOError:
            return '上传文件失败'
        return '上传文件成功, 文件名: {}'.format(file_name)
    else:
        return '上传文件失败'

@route('/favicon.ico', method='GET')
def server_static():
    """处理网站图标文件, 找个图标文件放在脚本目录里"""
    return static_file('favicon.ico', root=base_path)


@error(404)
def error404(error):
    """处理错误信息"""
    return '404 发生页面错误, 未找到内容'

run(port=8080, reloader=False)  # reloader设置为True可以在更新代码时自动重载

使用python 2.7 在DOS窗口 pip install bottle 或者下载一个bottle.py放在脚本同一个目录。 https://pypi.python.org/pypi/bottle/0.12.8

37,744

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧