Pythonflask webapp根据用户选择汇总了许多大文件,但用户不知道它是否被挂起

问题描述 投票:0回答:1

运行烧瓶Python Web应用程序,该应用程序将许多文件汇总并根据用户的过滤器将其提供给用户。 sissue,用户单击下载后,后端拉动了所有文件,ZIP创建开始,但这可能需要几分钟。用户不知道它是否被悬挂。

I决定将其创建时流式zip文件更快地将文件传输给用户,并且还可以让用户知道Web应用程序正在使用它。此问题是为了使用浏览器的下载部分(Little Pop UP或带有Progress Bars的下载页面),您需要提供内容长度的标题,但是我们不知道zip文件的大小,因为它尚未完成。我尽力估算zip文件完成后的大小,我认为这很容易,因为我的zip只是zip_stored,但是我无法准确测量内部ZIP结构。浏览器最终使用err_content_length_mismatch拒绝下载。

i可以提供服务器发送的事件(SSE)路由,以通过阅读在单独 /进度路线中发送的字节和对其进行轮询的字节数来制作我自己的进度标准在这一点上为我感到自豪。我也可能不会流式传输它,然后当它创建时,请使用SSE来提供zip上的更新,然后用内容长度的标题发送到它后才发送它...虽然不如我希望的那样好.

def calculate_total_size(filenames): total_size = 0 for file in filenames: matching_filepath, _ = get_file_info(file) if matching_filepath: total_size += os.path.getsize(matching_filepath) # Add overhead for ZIP file structure (22 bytes per file + 22 bytes for the central directory) total_size += 22 * (len(filenames) + 1) return total_size def generate_file_entries(filenames): for file in filenames: matching_filepath, filename = get_file_info(file) if matching_filepath: file_stat = os.stat(matching_filepath) modified_at = datetime.utcfromtimestamp(file_stat.st_mtime) with open(matching_filepath, 'rb') as f: chunk = f.read() if isinstance(chunk, bytes): # Ensure only bytes are yielded yield filename, modified_at, 0o600, ZIP_64, [chunk] else: print(f"Unexpected data type for file contents: {type(chunk)}")

您尝试使用不需要给出完整内容长度的传输方法?像这样的东西
python web-applications http-headers zip content-length
1个回答
0
投票

© www.soinside.com 2019 - 2024. All rights reserved.