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

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?

Related

How to Get File Content from basecamp project tool

how to get file content in python which is already uploaded or created at basecamp project management tool
I have try from python get response but content not read from specific file
code
import requests
file = requests.get('https://3.basecamp.com/5560642/buckets/31552935/documents/5836740028')
print(file)
files = open("https://3.basecamp.com/5560642/buckets/31552935/documents/5836740028", "r")
print(files.read())

Download generated excel file using xslxwriter on Python(Django)

I am working with Python using the Django framework, at this moment I am generating some reports in Excel, for that I use the xslxwriter library, the user tries to download the file from the platform, I do not get the download and instead it The answer is strange characters, I think that's the Excel file, anyway I don't know how to make that file download.
This snippet is the one that is supposed to download the file
workbook.close()
output.seek(0)
response = HttpResponse(output.read(), content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
response['Content-Disposition'] = "attachment; filename=test.xlsx"
output.close()
return response
This is the response I get from the frontend
Thank you very much in advance.

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

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.

Can't open downloaded .mp4 files downloaded with requests and urllib

I've downloaded some files using requests
url = 'https://www.youtube.com/watch?v=gp5tziO5lXg&feature=youtu.be'
video_name = url.split('/')[-1]
print("Downloading file:%s" % video_name)
# download the url contents in binary format
r = requests.get(url)
# open method to open a file on your system and write the contents
with open('saved.mp4', 'wb') as f:
f.write(r.content)
and using urllib.requests
url = 'https://www.youtube.com/watch?v=gp5tziO5lXg&feature=youtu.be'
video_name = url.split('/')[-1]
print("Downloading file:%s" % video_name)
# Copy a network object to a local file
urllib.request.urlretrieve(url, "saved2.mp4")
When I then try to open the .mp4 file I get the following error
Cannot play
This file cannot be played. This can happen because the file type is
not supported, the file extension is incorrect or the file is
corrupted.
0xc00d36c4
If I test it with pytube it works fine.
What's wrong with the other methods?
To answer your question, with the other methods it is not downloading the video but the page. What you may be obtaining is an html file with an mp4 file extension.
Therefore, it gives that error when trying to open the file.
If pytube works for what you need, I would suggest using that one.
If you want to download videos from other platforms, you might consider youtube-dl.
Hello you can import IPython.display for audio diplay
import IPython.display as ipd
ipd.Audio(video_name)
regards
I hope I can have solved your problem

PDF files downloaded with Python cannot be opened in acrobat

I have a little python script that I am using to download a whole bunch of PDF files for archiving. The problem I have is that when I download the files, they appear correctly under the correct title, but they are the wrong size and they can't be opened by Acrobat, which fails with an error message saying Out of memory or Insufficient data for an image or some other arbitrary Acrobat error. Viewing the content of the page in a text editor looks a bit like a PDF document, by which I mean it is incomprehensible in general but with a few fragments of text and markup, including PDF identifiers.
The code to download the file is this:
def download_file( file_id):
folder_path = ".\\pdf_files\\"
file_download="http://myserver/documentimages.asp?SERVICE_ID=RETRIEVE_IMAGE&documentKey="
file_content = urllib.urlopen(file_download+file_id, proxies={})
file_local = open( folder_path + file_id + '.pdf', 'w' )
file_local.write(file_content.read())
file_content.close()
file_local.close()
If the same file is downloaded through a browser it looks fine, but is also larger on the disk. I am guessing that the problem might be to do with the encoding of the file when it is saved?
You need to write it as a binary file so:
file_local = open( folder_path + file_id + '.pdf', 'wb' )

Categories

Resources