Web. py large file download

Source: Internet
Author: User

When you need to download a large file, if you read the file directly before returning it, it will be a waste of memory and even crash.

So we need to read some content and flush it directly to the client, but the flush method is not found in the web. py document.

However, you can view the yield returned content in how to stream large files in the cookbook of Web. py. Therefore, we can use yield for flush.

Buf_size = 262144 class download: def get (Self): file_name = 'file _ name' file_path = OS. path. join ('file _ path', file_name) F = none try: F = open (file_path, "rb") webpy. header ('content-type', 'application/octet-stream') webpy. header ('content-disposition', 'attachment; filename = % S. dat '% file_name) While true: c = f. read (buf_size) If C: yield C else: Break failed t exception, E: Print e yield 'error' finally: If F: f. close ()

OK!

Bird!

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.