Flask 上传文件
后端
@app.route('/index/', methods=['GET', 'POST']) def index(): import os if request.method == 'GET': return render_template('index.html') # POST file_obj = request.files.get('code') # print(file_obj) # <FileStorage: 'bbs.zip' ('application/zip')> # print(file_obj.filename) # bbs.zip print(type(file_obj)) # <class 'werkzeug.datastructures.FileStorage'> # from werkzeug.datastructures import FileStorage # 查看源码 file_path = os.path.join(os.getcwd(), 'files', file_obj.filename) # print(file_path) file_obj.save(file_path) # save(self, dst, buffer_size=16384) dst是目标文件,包括文件名 return '上传成功'
前端
<form action="" method="post" enctype="multipart/form-data"> <p><input type="file" name="code" id=""></p> <p><input type="submit" name="" value="上传"></p> </form>