python3 接收html頁面上傳的文件


code

# 文件上傳
import flask, os, sys,time
from flask import request


interface_path = os.path.dirname(__file__)
sys.path.insert(0, interface_path)  #將當前文件的父目錄加入臨時系統變量


server = flask.Flask(__name__, static_folder='static')


@server.route('/', methods=['get'])
def index():
    return '<form action="/upload" method="post" enctype="multipart/form-data"><input type="file" id="img" name="img"><button type="submit">上傳</button></form>'


@server.route('/upload', methods=['post'])
def upload():
    fname = request.files['img']  #獲取上傳的文件
    if fname:
        t = time.strftime('%Y%m%d%H%M%S')
        new_fname = r'static/' + t + fname.filename
        fname.save(new_fname)  #保存文件到指定路徑
        return '<img src=%s>' %  new_fname
    else:
        return '{"msg": "請上傳文件!"}'
print('----------路由和視圖函數的對應關系----------')
print(server.url_map) #打印路由和視圖函數的對應關系
server.run(port=8000)

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM