Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 通过AJAX返回ZIP文件_Jquery_Python_Ajax_Flask_Zip - Fatal编程技术网

Jquery 通过AJAX返回ZIP文件

Jquery 通过AJAX返回ZIP文件,jquery,python,ajax,flask,zip,Jquery,Python,Ajax,Flask,Zip,我需要下载通过AJAX触发的文件。我的代码从临时文件夹中的URL检索图像,并在此临时文件夹中创建文件的内存zip。我想将文件路径推送到客户端。我该怎么做 这是我的压缩代码: #This function creates a temp folder, downloads images in it and returns the path temp_dir = retrieve_images(file_paths) in_memory_loc = io.BytesIO() zipf = zipf

我需要下载通过AJAX触发的文件。我的代码从临时文件夹中的URL检索图像,并在此临时文件夹中创建文件的内存zip。我想将文件路径推送到客户端。我该怎么做

这是我的压缩代码:

#This function creates a temp folder, downloads images in it and returns the path
temp_dir = retrieve_images(file_paths)

in_memory_loc = io.BytesIO()

zipf = zipfile.ZipFile(, 'w')
zipdir(temp_dir, zipf)
zipf.close()

#remove the temp directory
shutil.rmtree(temp_dir)

in_memory_loc.seek(0)

#output the zip file to the browser
return send_file(in_memory_loc, attachment_filename='site_images.zip', as_attachment=False)

当我使用传统的表单提交方式时,它就起作用了。但是我需要通过AJAX返回这个。通过AJAX返回文件似乎是不可能的,因此我想返回zip文件路径。我该怎么做?

创建一个指向该资源的url并将其发送回ajax,使用
窗口触发下载。打开('url')
;在成功函数中

您有临时目录的名称和文件的名称,您有构建url所需的所有内容,因此服务器必须保留zip文件的内容,并等待客户端发出第二个HTTP请求以执行下载

一种方法是将内容存储在memcache中的一个随机生成的密钥下,然后返回带有XHR响应的密钥。然后,客户端可以在非XHR请求中将该密钥传递回服务器以请求该文件。

(没有ajax)
您可以使用Jquery中的窗口位置单击功能下载zip文件

 $(this).ready(function() {
        $("#downloadBtn").click(function() {
            var formdata= $("#formId").serialize();
            window.location="download.action?"+formdata;
        });
    });

这正是我被困的地方。如何创建指向内存中位置的URL?如何创建指向内存中位置的URL?或者,我如何告诉zipfile将该拉链存储在同一临时位置?抱歉,我是新手。我可以考虑从这个问题中删除代码和Python标签,因为我可以说这是一个与Python或zip文件无关的一般Web问题。是的,但是我使用的语言是Python。而且人们通常倾向于为他们正在编码的语言找到答案!