Iframe 未加载 pdf 内容

问题描述 投票:0回答:1

我正在尝试在 iframe 中加载 pdf,我进行后端调用来获取文件名和文件路径。

然后我将该路径传递到前端以在 src 标记内渲染,但随后它提到了一些内容。

Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

文件路径:

@auth.route("/building/view-file")
def view_file():
filename = request.args.get("file")
building = request.args.get("building")
document = Document.query.filter_by(name=filename).first()
print(document.path)
return render_template(
    "building/Building/view_file.html", document=document.path, building=building
)

HTML代码:

<iframe src="{{ document }}"></iframe>
<button class="tenant_card_buttons" hx-get="/auth/building-info-expanded" hx- 
vals='{"building":"{{building}}"}'
hx-target="#modal">Back</button>
<button class="tenant_card_buttons" onclick="closeModal()">Close</button>

服务器日志:

FILE_STORAGE/Matric_Certificate.pdf
127.0.0.1 - - [28/Apr/2024 17:23:33] "GET /auth/building/view-file? 
file=Certificate.pdf&building=1 HTTP/1.1" 200 -
127.0.0.1 - - [28/Apr/2024 17:23:33] "GET /auth/FILE_STORAGE/Matric_Certificate.pdf 
HTTP/1.1" 404 -

我做错了什么?为什么src然后再次查询db?

python html iframe
1个回答
0
投票

问题可能在于您如何将路径传递到前端。正确的做法可能是:

return render_template(
    "building/Building/view_file.html", document=url_for('static', filename=document.path), building=building
)
© www.soinside.com 2019 - 2024. All rights reserved.