这是我的问题。我需要创建一个网络应用程序,该应用程序能够上传两个单独类型的文件(分别是wav文件和midi / MusicXML文件)。现在这行不通。 这是我的代码:
app.py
flask import Flask
UPLOAD_FOLDER = '/Users/CatLover/Documents/DataScience/Insight/FrontEnd/Kitty/files/'
app = Flask(__name__)
app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
main.py
导入操作系统
#导入魔术
导入urllib.request
从应用程序导入应用程序
从flask导入Flask,flash,请求,重定向,render_template
从werkzeug.utils导入secure_filename
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def upload_form():
return render_template('upload.html')
@app.route('/', methods=['POST'])
def upload_file():
if request.method == 'POST':
print(request.__dict__)
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No file selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
flash('File successfully uploaded')
return redirect('/')
else:
flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif')
return redirect(request.url)
if __name__ == "__main__":
app.run(host="127.0.0.1",port=int("5003"),debug=True)
templates/upload.html
:
<!doctype html>
<title>Python Flask File Upload Example</title>
<h2>Step 1: Select a sheet music file (.xml, .mxl or .mid) to upload</h2>
<p>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
</p>
<form method="post" name="sform" action="/" enctype="multipart/form-data">
<dl>
<p>
<input type="file" name="sfile" autocomplete="off" required>
</p>
</dl>
<p>
<input type="submit" name="ssubmit" value="Submit">
</p>
</form>
<h2>Step 2: Select a wav file to upload</h2>
<form method="post" name="wform" action="/" enctype="multipart/form-data">
<dl>
<p>
<input type="file" name="wfile" autocomplete="off" required>
</p>
</dl>
<p>
<input type="submit" name="wsubmit" value="Submit">
</p>
</form>
答案 0 :(得分:0)
请确保在main.py中使用相同的输入字段名称(在upload.html中提到) 这是有效的解决方案,该解决方案将仅采用'xml','mxl','mid','wav'扩展名, 我们可以根据需要删除多个上传字段,但是根据您的要求,我进行了修改。 谢谢
app.py
android.view.InflateException: Binary XML file line #32: Binary XML file line #32: You must supply a layout_height attribute.
Caused by: java.lang.UnsupportedOperationException: Binary XML file line #32: You must supply a layout_height attribute.
main.py
from flask import Flask
UPLOAD_FOLDER = '/root/Flask'
app = Flask(__name__)
app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
templates / upload.html
import os
import magic
from app import app
from flask import Flask, flash, request, redirect, render_template
from werkzeug.utils import secure_filename
ALLOWED_EXTENSIONS = set(['xml', 'mxl','mid','wav'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def upload_form():
return render_template('upload.html')
@app.route('/', methods=['POST'])
def upload_file():
if request.method == 'POST':
print(request.__dict__)
# check if the post request has the file part
if 'sfile' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['sfile']
if file.filename == '':
flash('No file selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
flash('File successfully uploaded')
return redirect('/')
else:
flash('Allowed file types are xml,mxl,mid,wav')
return redirect(request.url)
if __name__ == "__main__":
app.run(host="0.0.0.0",port=int("5003"),debug=True)