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!