Home > OS >  Flask send_file() working with pdf but not with html
Flask send_file() working with pdf but not with html

Time:12-23

I'm having trouble with a flask azure app. I have some files saved on the storage (pdfs and htmls) and I need to return these files when I invoke the get_file_safe endpoint. This method takes a file_id parameter and accesses the database, goes to blob azure, creates a temporary file and returns that file. When I pass codes that refer to PDF files, it works perfectly and the file is displayed on the screen. When the code matches an HTML file the answer is blank. Does anyone have any idea what it might be? Thank you very much ! (Note: When I used GCP it worked but I had to migrate, so I put here that it is 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)   

CodePudding user response:

As you mentioned only html files not able to read so I tried with html file reading temporary file display it on the browser

I tried with tempfile.NamedTemporaryFile() as temp: but getting the black page And then I also tried with with tempfile.NamedTemporaryFile('w', delete=False, suffix='.html') as f: And I write data as string able to get the page

Can you just try with tempfile.NamedTemporaryFile('w', delete=False, suffix='.html') as f: for html files

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)

Here is the OUTPUT how it looks

enter image description here

enter image description here

  • Related