上传按钮会在所需位置提供上传文件的副本但我也可以在下载目录中获取该文件。
我正在尝试将文件上传到所需的位置,文件路径有一些功能可以正常工作但我得到一个框要求保存文件或取消,单击保存按钮将文件存储在下载文件夹中我不想要
import os
from flask import Flask, render_template, request, redirect, url_for, \
send_from_directory
from werkzeug.utils import secure_filename
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = '/home/personal/mytweets'
app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload():
# Get the name of the uploaded file
file = request.files['file']
# Check if the file is one of the allowed types/extensions
if file and allowed_file(file.filename):
# Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file', filename=filename))
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=int("80"), debug=True)
-------的index.html ------------------
enter code here
<!DOCTYPE html>
<html lang="en">
<body>
<div class="container">
<div class="header">
<h3 class="text-muted">Upload a Test File</h3>
</div>
<div>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
</div>
</div>
</body>
</html>