Download excel to open in browser instead of downloading directly? Python & Flask - python

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

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.

Related

How to open the latest downloaded file

This is the code immediately before I get stuck:
#Start a new post
driver.find_element_by_xpath("//span[normalize-space()='Start a post']").click()
time.sleep(2)
driver.find_element_by_xpath("//li-icon[#type='image-icon']//*[name()='svg']").click()
time.sleep(2)
The above code works well. The next step though has me puzzled.
I would like to upload the latest image file from my downloads folder. When I click on the link above in LinkedIn it navigates to my user folder (Melissa). (see image)
So... I'm looking for the next lines of code to navigate from the default folder to the Downloads folder (C:\Users\Melissa\Downloads), then, select the newest file, then click 'open' so it attaches to the LinkedIn post.
You can upload the image using this method:
import getpass, glob, os
# Replace this with the code to get the upload input tag
upload_button = driver.find_element_by_xpath("//input[#type='file']")
# Get downloads
downloads = glob.glob("C:\\Users\\{}\\Downloads\\*".format(getpass.getuser()))
# Find the latest downloaded file
latest_download = max(downloads, key=os.path.getctime)
# Enter it into the upload input tag
upload_button.send_keys(latest_download)

Create a pdf with Python Flask from file

I need to write a python flask service which takes in input a file (.doc or .ppt) and returns the same file converted in pdf without saving it in a folder. This service has to run on a server.
I tried with pdfkit but I've it not easy to install it on a server.
What i need is something like this:
def post(self):
file = request.files['file_request']
pdf = pdfkit.from_file(file, False) #or any other library to convert the file
return send_file(
pdf,
mimetype='application/application/pdf',
attachment_filename="File.pdf",
as_attachment=True,
cache_timeout=0
)
Is it possible to do it? Could somebody help me?
Thanks

Flask: force download pdf files to open in browser

I'm trying to download a PDF file using flask but I don't want the file to download as an attachment. I simply want it to appear in the user's browser as a separate webpage. I've tried passing the as_attachment=False option to the send_from_directory method but no luck.
Here is my function so far:
#app.route('/download_to_browser')
def download_to_browser(filename):
return send_from_directory(directory=some_directory,
filename=filename,
as_attachment=False)
The function works in the sense that the file is downloading to my computer but I'd much rather just display it in the browser (and let the user download the file if they want to).
I read here I need to change thecontent-disposition parameter but I'm not sure how that can be done efficiently (perhaps using a custom response?). Any help?
Note: I'm not using Flask-Uploads at the moment but I probably will down the line.
You can try to add the mimetype parameter to send_from_directory:
return send_from_directory(directory=some_directory,
filename=filename,
mimetype='application/pdf')
That works for me, at least with Firefox.
If you need more control over headers, you can use a custom response, but you will lose the advantages of send_file() (I think it does something clever to serve the file directly from the webserver.)
with open(filepath) as f:
file_content = f.read()
response = make_response(file_content, 200)
response.headers['Content-type'] = 'application/pdf'
response.headers['Content-disposition'] = ...
return response

Django: MP3 file being opened, not saved, on Apple devices

I am working with Python and Django 1.9.
I manage to serve an MP3 file for download and it works fine on my computer and on my Android phone. However, on my iPad, the file isn't downloaded and saved as a file; it is directly opened in both the Safari and Chrome browsers.
Is there any way to save the file on the iPad?
This is the code snippet that I'm using:
fsock = open(filename, 'r')
response = HttpResponse(fsock, content_type='audio/mpeg')
response['Content-Disposition'] = "attachment; filename=%s.mp3" % (filename)
return response
Is there something I should change in my code or is it rather a constraint from Apple's OS to avoid saving downloaded MP3 files directly from the browser?

How do I allow users to download a MIDI file with Flask without getting a 0 byte download?

I am writing an application that creates a midi file using the MIDIUtil library. When the user submits an HTML form, a midi file object is created with MIDIUtil. How do I allow the user to download this as a .mid file? I have tried the following code, but I end up downloading a file of 0 bytes.
return Response(myMIDIFile, mimetype='audio/midi')
I use a variant of the following code to allow my users to download images they generate. The below code should work for you. Please note that you will most likely need to specify the full server path to the file being downloaded.
from flask import send_file
download_filename = FULL_PATH_TO_YOUR_MIDI_FILE
return(send_file(filename_or_fp = download_filename,mimetype="audio/midi",as_attachment=True))
I ended up using this, and it worked.
new_file = open('test.mid', 'wb')
myMIDI.writeFile(new_file)
new_file.close()
new_file = open('test.mid', 'rb')
return send_file(new_file, mimetype='audio/midi')
Might want to just try using send_file
from flask import send_file
return send_file("yourmidifile.mid", as_attachement=True, mimetype="audio\midi")

Categories

Resources