0
votes

Can I make an excel file open in the browser to be viewed instead of being downloaded, and have a download button, similar to how PDFs are?

I'm using Python Flask, for PDFs I do:

@blueprint_name.route("/download_some_pdf", methods=["GET"])
def download_pdf():
  return send_file(file_path, cache_timeout=1)

This opens the PDF in a browser tab with the download PDF button

and for Excel files:

@blueprint_name.route("/download_some_xlsx", methods=["GET"])
def download_xlsx():
  return send_from_directory(dir_path, filename, as_attachment=True, cache_timeout=1)

If for the Excel I remove the as_attachment parameter, or I use send_file instead of send_from_directory, it still downloads the file but with the name of the method ("download_xlsx") instead of the filename!!

I'm using Python 3.8.3 and Flask 1.1.2

1
Most browsers have a built-in PDF viewer. Most do not have a built-in Excel viewer, so unless you render the Excel file in HTML yourself, you’re unlikely to have a representation that can be opened in a browser.ig0774
Perhaps then there is an HTML viewer for excels that is as simple to use as the Google PDF viewer which comes automatically with download btn and navigation?yldm

1 Answers

0
votes

So far as I know, whether to open the file depends on the client browser. I have encountered a browser who could not read the pdf and download it directly. So once you set the file to have the "GET" property it is upon the receiver to open it or simply save it in directory. Moreover, the "open pdf" only happens after the file is already fully downloaded in Temporary directory set by the browser.