Flask send_file() 使用 pdf 但不使用 html
Flask send_file() working with pdf but not with html
我在使用 Flask + Azure 应用程序时遇到问题。我在存储中保存了一些文件(pdf 和 html),当我调用 get_file_safe 端点时我需要 return 这些文件。此方法采用 file_id 参数并访问数据库,转到 blob azure,创建临时文件并 returns 该文件。当我传递引用 PDF 文件的代码时,它运行良好并且文件显示在屏幕上。当代码匹配 HTML 文件时,答案为空。有谁知道它可能是什么?非常感谢你 ! (注意:当我使用 GCP 时它可以工作但我必须迁移,所以我把它放在这里是天蓝色的)。
from flask import Flask, flash, jsonify, session, redirect, url_for, escape, request, render_template, session, send_file
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__, ContentSettings
def get_file_safe():
#login and security stuff (...) Logic goes here ->>>
file_id = request.args.get('file_id')
cursor.execute(
"""SELECT link, mimetype from TABLE where id = %s """, (file_id))
rows = cursor.fetchall()
link = rows[0][0]
mimetype = rows[0][1]
filename = link.split("/")[-1]
print("Filename{}".format(filename))
print("Mimetype {}".format(mimetype))
# google cloud version, commented
#client = storage.Client()
#bucket = client.get_bucket('BUCKET_NAME')
#blob = bucket.blob(link)
#with tempfile.NamedTemporaryFile() as temp:
# blob.download_to_filename(temp.name)
# return send_file(temp.name, attachment_filename=filename)
# azure verson
bucket_name = 'BUCKET-NAME'
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
blob_client = blob_service_client.get_blob_client(container=bucket_name, blob=link)
with tempfile.NamedTemporaryFile() as temp:
temp.write(blob_client.download_blob().readall())
#return send_file(temp.name, attachment_filename=filename, mimetype=mimetype)
return send_file(temp.name, download_name=filename)
正如您提到的只有 html 文件无法读取所以我尝试使用 html 文件读取临时文件在浏览器上显示它
我试过 tempfile.NamedTemporaryFile() as temp:
但得到的是黑页
然后我也尝试了 with tempfile.NamedTemporaryFile('w', delete=False, suffix='.html') as f:
我将数据写为能够获取页面的字符串
你能不能用 tempfile.NamedTemporaryFile('w', delete=False, suffix='.html') as f:
试试 html 个文件
from azure.storage.blob import BlobServiceClient
import tempfile
import webbrowser
blob_service_client = BlobServiceClient.from_connection_string("Connection String ")
# Initialise container
blob_container_client = blob_service_client.get_container_client("test")
# Get blob
blob_client = blob_container_client.get_blob_client("test.html")
print("downloaded the blob ")
# Download
str=blob_client.download_blob().readall()
print(str)
print(str.decode("utf-8"))
//Getting the Blank Page
with tempfile.NamedTemporaryFile() as temp:
url = 'file://' + temp.name
temp.write(blob_client.download_blob().readall())
#temp.write(str)
webbrowser.open(url)
//Getting page
html=str.decode("utf-8")
with tempfile.NamedTemporaryFile('w', delete=False, suffix='.html') as f:
url = 'file://' + f.name
f.write(html)
webbrowser.open(url)
这是输出它的样子
我在使用 Flask + Azure 应用程序时遇到问题。我在存储中保存了一些文件(pdf 和 html),当我调用 get_file_safe 端点时我需要 return 这些文件。此方法采用 file_id 参数并访问数据库,转到 blob azure,创建临时文件并 returns 该文件。当我传递引用 PDF 文件的代码时,它运行良好并且文件显示在屏幕上。当代码匹配 HTML 文件时,答案为空。有谁知道它可能是什么?非常感谢你 ! (注意:当我使用 GCP 时它可以工作但我必须迁移,所以我把它放在这里是天蓝色的)。
from flask import Flask, flash, jsonify, session, redirect, url_for, escape, request, render_template, session, send_file
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__, ContentSettings
def get_file_safe():
#login and security stuff (...) Logic goes here ->>>
file_id = request.args.get('file_id')
cursor.execute(
"""SELECT link, mimetype from TABLE where id = %s """, (file_id))
rows = cursor.fetchall()
link = rows[0][0]
mimetype = rows[0][1]
filename = link.split("/")[-1]
print("Filename{}".format(filename))
print("Mimetype {}".format(mimetype))
# google cloud version, commented
#client = storage.Client()
#bucket = client.get_bucket('BUCKET_NAME')
#blob = bucket.blob(link)
#with tempfile.NamedTemporaryFile() as temp:
# blob.download_to_filename(temp.name)
# return send_file(temp.name, attachment_filename=filename)
# azure verson
bucket_name = 'BUCKET-NAME'
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
blob_client = blob_service_client.get_blob_client(container=bucket_name, blob=link)
with tempfile.NamedTemporaryFile() as temp:
temp.write(blob_client.download_blob().readall())
#return send_file(temp.name, attachment_filename=filename, mimetype=mimetype)
return send_file(temp.name, download_name=filename)
正如您提到的只有 html 文件无法读取所以我尝试使用 html 文件读取临时文件在浏览器上显示它
我试过 tempfile.NamedTemporaryFile() as temp:
但得到的是黑页
然后我也尝试了 with tempfile.NamedTemporaryFile('w', delete=False, suffix='.html') as f:
我将数据写为能够获取页面的字符串
你能不能用 tempfile.NamedTemporaryFile('w', delete=False, suffix='.html') as f:
试试 html 个文件
from azure.storage.blob import BlobServiceClient
import tempfile
import webbrowser
blob_service_client = BlobServiceClient.from_connection_string("Connection String ")
# Initialise container
blob_container_client = blob_service_client.get_container_client("test")
# Get blob
blob_client = blob_container_client.get_blob_client("test.html")
print("downloaded the blob ")
# Download
str=blob_client.download_blob().readall()
print(str)
print(str.decode("utf-8"))
//Getting the Blank Page
with tempfile.NamedTemporaryFile() as temp:
url = 'file://' + temp.name
temp.write(blob_client.download_blob().readall())
#temp.write(str)
webbrowser.open(url)
//Getting page
html=str.decode("utf-8")
with tempfile.NamedTemporaryFile('w', delete=False, suffix='.html') as f:
url = 'file://' + f.name
f.write(html)
webbrowser.open(url)
这是输出它的样子