Python + django implements file download,

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.