weixin_33720452 2016-04-12 07:05 采纳率: 0%
浏览 378

通过AJAX返回ZIP文件

I need to download files, which is triggered via AJAX. My code retrieves images from URLs in a temp folder, and creates an in memory zip of the the files in this temp folder. I want to push the file path to the client. How would I do it?

Here is my zipping code:

#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)

This works when I use the traditional form submit way. But I need to return this over AJAX. Returning a file over AJAX seems impossible, hence I want to return the zip file path. How would I do it?

  • 写回答

2条回答 默认 最新

  • MAO-EYE 2016-04-12 07:11
    关注

    Create a url to that resource and send it back to the ajax, trigger the download with window.open('url'); in the success function

    You have the name of the temp directory and the name of the file you have all that you need to build a url

    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan