Browse Source

feat: 添加下载功能

SongZihuan 3 năm trước cách đây
mục cha
commit
244171bd90
2 tập tin đã thay đổi với 14 bổ sung1 xóa
  1. 1 0
      templates/docx/article.html
  2. 13 1
      view/docx.py

+ 1 - 0
templates/docx/article.html

@@ -18,6 +18,7 @@
                     {% for archive in article.archive %}
                         <span class="badge badge-info"> {{ archive.name }} </span>
                     {% endfor %}
+                    <a href="{{ url_for('docx.article_down_page', blog_id=article.blog_id) }}"> 下载 </a>
                     <hr>
 
                     {{ article.context | safe }}

+ 13 - 1
view/docx.py

@@ -1,4 +1,4 @@
-from flask import Flask, Blueprint, render_template, abort, redirect, url_for, flash
+from flask import Flask, Blueprint, render_template, abort, redirect, url_for, flash, make_response
 from flask_wtf import FlaskForm
 from flask_pagedown import PageDown
 from flask_pagedown.fields import PageDownField
@@ -82,6 +82,18 @@ def article_page(blog_id: int):
                            show_email=current_user.check_role("ReadUserInfo"))
 
 
+@docx.route('/down/<int:blog_id>')
+def article_down_page(blog_id: int):
+    article = load_blog_by_id(blog_id)
+    if article is None:
+        abort(404)
+        return
+
+    response = make_response(article.context)
+    response.headers["Content-Disposition"] = f"attachment; filename={article.title}.html"
+    return response
+
+
 @docx.route('/comment/<int:blog>', methods=["POST"])
 @login_required
 def comment_page(blog: int):