0

我正在尝试使用烧瓶上传视频并在 html 页面中播放,但问题是它没有播放。我使用了下面的代码:

UPLOAD_FOLDER = 'static/'

@app.route('/')
def upload_form():
    return render_template('upload.html')


@app.route('/', methods=['POST'])
def upload_video():
    if 'file' not in request.files:
        flash('No file part')
        return redirect(request.url)
    file = request.files['file']
    if file.filename == '':
        flash('No image selected for uploading')
        return redirect(request.url)
    else:
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        # print('upload_video filename: ' + filename)
        flash('Video successfully uploaded and displayed below')
        return render_template('upload.html', filename=filename)


@app.route('/display/<filename>')
def display_video(filename):
    # print('display_video filename: ' + filename)
    return redirect(url_for('static', filename=filename), code=301)
if __name__ == '__main__':
    app.run(threaded=True)

在 html 文件中:

<p>
    {% with messages = get_flashed_messages() %}
      {% if messages %}
        <ul>
        {% for message in messages %}
          <li>{{ message }}</li>
        {% endfor %}
        </ul>
      {% endif %}
    {% endwith %}
</p>
{% if filename %}
    <div style="margin: 10px auto;">
        <video autoplay="autoplay" controls="controls" preload="preload">
            <source src={{ url_for('display_video', filename=filename) }} type="video/mp4"></source>
        </video>
    </div>
{% endif %}
<form method="post" action="/" enctype="multipart/form-data">
    <dl>
        <p>
            <input type="file" name="file" autocomplete="off" required>
        </p>
    </dl>
    <p>
        <input type="submit" value="Upload">
    </p>
</form>

视频已上传但无法播放,我无法播放播放按钮或与之互动

4

1 回答 1

0

我认为您应该使用send_from_directory使文件可访问。

@app.route("/display/<path:filename>")
def display_video(filename):
    return send_from_directory(
        app.config['UPLOAD_FOLDER'], filename, as_attachment=True
    )

我还在源标签中添加了引号。

<video autoplay="autoplay" controls="controls" preload="preload">
  <source src="{{ url_for('display_video', filename=filename) }}" type="video/mp4"></source>
</video>
于 2021-08-03T19:59:45.557 回答