Python + django implements file download,
(1) Method 1: directly use the file address in the href + database of tag a to download the file. Disadvantage: word excel is directly downloaded in a pop-up box. For image txt and other files, the download method is directly opened on the new page.
(2) Method 2: process the input items of the downloaded content in the python background, and a download box is displayed for the returned content.
# Background processing function def downloadFile (req): filename = basePath + req. GET ['url'] def file_iterator (file_name, chunk_size = 512): with open (file_name) as f: while True: c = f. read (chunk_size) if c: yield c else: break response = StreamingHttpResponse (file_iterator (filename )) response ['content-type'] = 'application/octet-stream' response ['content-disposition'] = 'attachment; filename = "{0 }"'. format (filename) return response
(3) Foreground Functions
① A tag calls the function input path <a href = '/downloadFile/url = path'>
②. The button label calls the jq method to call the background function.
<input type='button' class='download'>
# Download button click event $ ("body"). on ("click", ". download", function () {3 location. href = "/downloadFile /? Url = "+ path ;});
Articles you may be interested in:
- Configuration illustration of Python + Django development environment in windows
- How to install python Django)
- Connect python Django to the MySQL database for addition, deletion, modification, and query
- How to Make Sublime 3 a powerful tool for Python/Django IDE development
- Tutorial on using the Python Django framework to complete video processing tasks
- Analyze the running mode and processing process of the Python Django framework
- Details about the templates settings in the Python Django framework
- Detailed explanation of Cookie-related processing in the Python Django framework
- Analysis of Memcached in Python Django framework
- Configuration documentary about deploying the Python Django framework with PyCharm
- Python + django implement File Upload