本文实例讲述了Python使用Flask框架同时上传多个文件的方法,分享给大家供大家参考。具体如下:
下面的演示代码带有详细的HTML页面和python代码
import os# We'll render HTML templates and access data sent by POST# using the request object from flask. Redirect and url_for# will be used to redirect the user once the upload is done# and send_from_directory will help us to send/show on the# browser the @R_403_6852@ that the user just uploadedfrom flask import Flask,render_template,request,redirect,url_for,send_from_directoryfrom werkzeug import secure_@R_403_6852@name# Initialize the Flask applicationapp = Flask(__name__)# This is the path to the upload directoryapp.config['UPLOAD_FolDER'] = 'uploads/'# These are the extension that we are accepting to be uploadedapp.config['ALLOWED_EXTENSIONS'] = set(['txt','pdf','png','jpg','jpeg','gif'])# For a given @R_403_6852@,return whether it's an allowed type or notdef allowed_@R_403_6852@(@R_403_6852@name): return '.' in @R_403_6852@name and \ @R_403_6852@name.rsplit('.',1)[1] in app.config['ALLOWED_EXTENSIONS']# This route will show a form to perform an AJAX request# jquery is loaded to execute the request and update the# value of the operation@app.route('/')def index(): return render_template('index.HTML')# Route that will process the @R_403_6852@ upload@app.route('/upload',methods=['POST'])def upload(): # Get the name of the uploaded @R_403_6852@s uploaded_@R_403_6852@s = request.@R_403_6852@s.getList("@R_403_6852@[]") @R_403_6852@names = [] for @R_403_6852@ in uploaded_@R_403_6852@s: # Check if the @R_403_6852@ is one of the allowed types/extensions if @R_403_6852@ and allowed_@R_403_6852@(@R_403_6852@.@R_403_6852@name): # Make the @R_403_6852@name safe,remove unsupported chars @R_403_6852@name = secure_@R_403_6852@name(@R_403_6852@.@R_403_6852@name) # Move the @R_403_6852@ form the temporal folder to the upload # folder we setup @R_403_6852@.save(os.path.join(app.config['UPLOAD_FolDER'],@R_403_6852@name)) # Save the @R_403_6852@name into a List,we'll use it later @R_403_6852@names.append(@R_403_6852@name) # Redirect the user to the uploaded_@R_403_6852@ route,which # will basicaly show on the browser the uploaded @R_403_6852@ # Load an HTML page with a link to each uploaded @R_403_6852@ return render_template('upload.HTML',@R_403_6852@names=@R_403_6852@names) # This route is expecting a parameter containing the name# of a @R_403_6852@. Then it will locate that @R_403_6852@ on the upload# directory and show it on the browser,so if the user uploads# an image,that image is going to be show after the upload@app.route('/uploads/<@R_403_6852@name>')def uploaded_@R_403_6852@(@R_403_6852@name): return send_from_directory(app.config['UPLOAD_FolDER'],@R_403_6852@name)if __name__ == '__main__': app.run( host="0.0.0.0",port=int("80"),deBUG=True )
index.HTML代码
<!DOCTYPE HTML><HTML lang="en"> <head> <link href="bootstrap/3.0.0/CSS/bootstrap.min.CSS" rel="stylesheet"> </head> <body> <div > <div > <h3 >How To Upload a @R_403_6852@.</h3> </div> <hr/> <div> <form action="upload" method="post" enctype="multipart/form-data"> <input type="@R_403_6852@" multiple="" name="@R_403_6852@[]" /><br/> <input type="submit" value="Upload" > </form> </div> </div> </body></HTML>
upload.HTML页面:
<!DOCTYPE HTML><HTML lang="en"> <head> <link href="bootstrap/3.0.0/CSS/bootstrap.min.CSS" rel="stylesheet"> </head> <body> <div > <div > <h3 >Uploaded @R_403_6852@s</h3> </div> <hr/> <div> This is a List of the @R_403_6852@s you just uploaded,click on them to load/download them <ul> {% for @R_403_6852@ in @R_403_6852@names %} <li><a href="{{url_for('uploaded_@R_403_6852@',@R_403_6852@name=@R_403_6852@)}}">{{@R_403_6852@}}</a></li> {% endfor %} </ul> </div> <div > <h3 >Code to manage a Upload</h3> </div> <hr/> <pre>@app.route('/upload',methods=['POST'])def upload(): # Get the name of the uploaded @R_403_6852@ #@R_403_6852@ = request.@R_403_6852@s['@R_403_6852@'] uploaded_@R_403_6852@s = request.@R_403_6852@s.getList("@R_403_6852@[]") @R_403_6852@names = [] for @R_403_6852@ in uploaded_@R_403_6852@s: # Check if the @R_403_6852@ is one of the allowed types/extensions if @R_403_6852@ and allowed_@R_403_6852@(@R_403_6852@.@R_403_6852@name): # Make the @R_403_6852@name safe,@R_403_6852@name)) @R_403_6852@names.append(@R_403_6852@name) # Redirect the user to the uploaded_@R_403_6852@ route,@R_403_6852@names=@R_403_6852@names)</pre> </div> </div> </body></HTML>
希望本文所述对大家的Python程序设计有所帮助。
总结以上是内存溢出为你收集整理的Python使用Flask框架同时上传多个文件的方法全部内容,希望文章能够帮你解决Python使用Flask框架同时上传多个文件的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)