运行
1 | python main.py |
访问
1 | http: //127.0.0.1:8987/upimg |
纯图片
main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | # coding:utf-8 from flask import Flask, render_template, request, redirect, url_for, make_response,jsonify from werkzeug.utils import secure_filename import os import cv2 import time from datetime import timedelta #设置允许的文件格式 ALLOWED_EXTENSIONS = set ([ 'png' , 'jpg' , 'JPG' , 'PNG' , 'bmp' ]) def allowed_file(filename): return '.' in filename and filename.rsplit( '.' , 1)[1] in ALLOWED_EXTENSIONS app = Flask(__name__) # 设置静态文件缓存过期时间 app.send_file_max_age_default = timedelta(seconds=1) # @app.route('/upload', methods=['POST', 'GET']) @app.route( '/upimg' , methods=[ 'POST' , 'GET' ]) # 添加路由 def upload(): #第一次访问直接返回upload.html if request.method == 'POST' :# 上传图片返回upload_ok.html f = request.files[ 'file' ] if not (f and allowed_file(f.filename)): return jsonify({ "error" : 1001, "msg" : "请检查上传的图片类型,仅限于png、PNG、jpg、JPG、bmp" }) user_input = request.form. get ( "name" ) basepath = os.path.dirname(__file__) # 当前文件所在路径 upload_path = os.path. join (basepath, 'static/images' , secure_filename(f.filename)) # 注意:没有的文件夹一定要先创建,不然会提示没有该路径 # upload_path = os.path.join(basepath, 'static/images','test.jpg') #注意:没有的文件夹一定要先创建,不然会提示没有该路径 f.save(upload_path) # 使用Opencv转换一下图片格式和名称 img = cv2.imread(upload_path) cv2.imwrite(os.path. join (basepath, 'static/images' , 'test.jpg' ), img) return render_template( 'upload_ok.html' ,userinput=user_input,val1=time.time()) # image_data = open(upload_path, "rb").read() # response = make_response(image_data) # response.headers['Content-Type'] = 'image/png' # return response return render_template( 'upload.html' ) if __name__ == '__main__' : # app.debug = True app.run(host= '0.0.0.0' , port=8987, debug=True) |
图片 和mp3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | # coding:utf-8 from flask import Flask, render_template, request, redirect, url_for, make_response,jsonify from werkzeug.utils import secure_filename import os import cv2 import time from datetime import timedelta #设置允许的文件格式 ALLOWED_EXTENSIONS = set ([ 'png' , 'jpg' , 'JPG' , 'PNG' , 'bmp' , 'mp3' , 'wav' ]) TYPE_IMG = set ([ 'png' , 'jpg' , 'JPG' , 'PNG' , 'bmp' ]) TYPE_VIDEO = set ([ 'mp3' , 'wav' ]) def allowed_file(filename): return '.' in filename and filename.rsplit( '.' , 1 )[ 1 ] in ALLOWED_EXTENSIONS app = Flask(__name__) # 设置静态文件缓存过期时间 app.send_file_max_age_default = timedelta(seconds = 1 ) # @app.route('/upload', methods=['POST', 'GET']) @app .route( '/upimg' , methods = [ 'POST' , 'GET' ]) # 添加路由 def upload(): #第一次访问直接返回upload.html if request.method = = 'POST' : # 上传图片返回upload_ok.html f = request.files[ 'file' ] filename = f.filename filetype = filename.rsplit( '.' , 1 )[ 1 ] print ( "文件名" ,filename, '文件格式' ,filetype) if not (f and allowed_file(filename)): return jsonify({ "error" : 1001 , "msg" : "请检查上传的文件类型,仅限于png、PNG、jpg、JPG、bmp、mp3" }) user_input = request.form.get( "name" ) print ("") basepath = os.path.dirname(__file__) # 当前文件所在路径 if filetype in TYPE_IMG: upload_path = os.path.join(basepath, 'static/images' , secure_filename(f.filename)) # 注意:没有的文件夹一定要先创建,不然会提示没有该路径 uploaddir_path = os.path.join(basepath, 'static/images' ,'') if not os.path.exists(uploaddir_path): print ( "不存在创建根目录" ) os.makedirs(uploaddir_path) # upload_path = os.path.join(basepath, 'static/images','test.jpg') #注意:没有的文件夹一定要先创建,不然会提示没有该路径 f.save(upload_path) # upload_path 保存原名 # 使用Opencv转换一下图片格式和名称 #img = cv2.imread(upload_path) #cv2.imwrite(os.path.join(basepath, 'static/images', 'test.jpg'), img) ''' userinput - 网页输入的文本 val1 - 时间戳判定是否超时 filename - 文件名字 ''' return render_template( 'upload_ok.html' ,userinput = user_input,val1 = time.time(),file_name = filename) #return jsonify({"errno": 0, "msg": "上传成功"}) # image_data = open(upload_path, "rb").read() # response = make_response(image_data) # response.headers['Content-Type'] = 'image/png' # return response elif filetype in TYPE_VIDEO: uploaddir_path = os.path.join(basepath, 'static/mp3' ,'') if not os.path.exists(uploaddir_path): print ( "不存在创建根目录" ) os.makedirs(uploaddir_path) upload_path = os.path.join(basepath, 'static/mp3' , filename) # 注意:没有的文件夹一定要先创建,不然会提示没有该路径 # upload_path = os.path.join(basepath, 'static/images','test.jpg') #注意:没有的文件夹一定要先创建,不然会提示没有该路径 print ( "上传音频位置" ,upload_path) f.save(upload_path) # upload_path 保存原名 return "上传音频成功:" + filename return render_template( 'upload.html' ) #return jsonify({"errno": 1, "msg": "file suffix not allowed "}) if __name__ = = '__main__' : # app.debug = True app.run(host = '0.0.0.0' , port = 8888 , debug = True ) |
templates
upload.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!DOCTYPE html> <html lang = "en" > <head> <meta charset = "UTF-8" > <title>上传文件系统< / title> < / head> <body> <h1>上传文件< / h1> <form action = "" enctype = 'multipart/form-data' method = 'POST' > < input type = "file" name = "file" style = "margin-top:20px;" / > <br> <i>请输入:< / i> < input type = "text" class = "txt_input" name = "name" value = " " style=" margin - top: 10px ;" / > < input type = "submit" value = "上传" class = "button-new" style = "margin-top:15px;" / > < / form> < / body> < / html> |
upload_ok.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <!DOCTYPE html> <html lang = "en" > <head> <meta charset = "UTF-8" > <title>上传文件系统< / title> < / head> <body> <h1>上传文件< / h1> <form action = "" enctype = 'multipart/form-data' method = 'POST' > < input type = "file" name = "file" style = "margin-top:20px;" / > <br> <i>请输入:< / i> < input type = "text" class = "txt_input" name = "name" value = " " style=" margin - top: 10px ;" / > < input type = "submit" value = "上传" class = "button-new" style = "margin-top:15px;" / > < / form> <h1>图片上传成功{{userinput}}!< / h1> <img src = "{{ url_for('static', filename= './images/'+file_name,_t=val1) }}" width = "640" height = "480" alt = "你的图片被外星人劫持了~~" / > < / body> < / html> |
static/images
第一个是上传的原图
test是用于返回网页使用的照片
分类:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2020-02-22 树媒派(4-4)flask和websocket 环境监测
2020-02-22 树莓派TCP通信
2020-02-22 python修改config