【问题标题】:Flask send_file() working with pdf but not with html烧瓶 send_file() 使用 pdf 但不使用 html
【发布时间】:2022-01-22 14:47:35
【问题描述】:

我在使用烧瓶 + 天蓝色应用时遇到问题。我在存储中保存了一些文件(pdf 和 html),我需要在调用 get_file_safe 端点时返回这些文件。此方法采用 file_id 参数并访问数据库,转到 blob azure,创建一个临时文件并返回该文件。当我传递引用 PDF 文件的代码时,它可以完美运行,并且文件显示在屏幕上。当代码与 HTML 文件匹配时,答案为空白。有谁知道它可能是什么?非常感谢 ! (注意:当我使用 GCP 时它可以工作,但我必须迁移,所以我在这里表示它是 azure)。

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)   

【问题讨论】:

    标签: python flask azure-blob-storage


    【解决方案1】:

    正如你提到的只有 html 文件无法读取,所以我尝试使用 html 文件读取临时文件将其显示在浏览器上

    我尝试使用tempfile.NamedTemporaryFile() as temp:,但得到了黑页 然后我也尝试了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)
    

    这是输出它的外观

    【讨论】:

    • 非常感谢!这对我来说很完美:)!
    • 感谢更新,您可以接受它作为答案(单击答案旁边的复选标记,将其从灰色切换为已填充。)。这对其他社区成员可能是有益的。谢谢
    • 嗨@EduardoCarusoBarbosaPacheco,您可以接受它作为答案(单击答案旁边的复选标记将其从灰色切换为已填充。)。这对其他社区成员可能是有益的。谢谢
    • 我做到了!谢谢
    猜你喜欢
    • 2016-01-08
    • 2019-05-13
    • 1970-01-01
    • 2014-12-09
    • 2021-08-19
    • 2019-09-11
    • 2021-04-25
    • 2021-10-17
    • 2018-10-09
    相关资源
    最近更新 更多