为什么threre是没有办法使用Ajax请求来下载文件? [英] Why threre is no way to download file using ajax request?

查看:215
本文介绍了为什么threre是没有办法使用Ajax请求来下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序,我们需要实现以下情形:

In our application we need to implement following scenario:

  1. 的请求被发送从客户
  2. 在服务器处理请求并生成文件
  3. 在服务器响应返回文件
  4. 在客户端浏览器中显示文件下载弹出对话框,允许用户下载文件

我们的应用程序是基于AJAX的应用程序,所以这将是非常容易和方便我们发送Ajax请求(如使用 jquery.ajax()函数)。

Our application is ajax based application, so it would be very easy and convenient for us to send ajax request (like using jquery.ajax() function).

但googilng后,事实证明,文件下载是使用非AJAX POST请求(如在的这个受欢迎的,所以线程)。因此,我们需要实现丑陋和更复杂的解决方案,所需的建筑HTML结构表格与嵌套的隐藏字段。

But after googilng, it turned out that file downloading is possible only when using non-ajax POST request (like described in this popular SO thread). So we needed to implement uglier and more complex solution that required building HTML structure of form with nested hidden fields.

在简单的话有人能解释这是为什么Ajax请求不能被用来下载文件?什么是机制的背后是什么?

Could someone explain in simple words why is that ajax requests cannot be used to download file? What's the mechanics behind that?

推荐答案

这不是AJAX。您可以下载当然,文件使用AJAX。但该文件将被保存在内存中,即你不能存盘。这是因为JavaScript不能与磁盘交互。这将是一个严重的安全问题,而且被阻止在所有的主流浏览器。

It's not about AJAX. You can download a file with AJAX, of course. However the file will be kept in memory, i.e. you cannot save file to disk. This is because JavaScript cannot interact with disk. That would be a serious security issue and it is blocked in all major browsers.

这篇关于为什么threre是没有办法使用Ajax请求来下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆