具有ajax的JSP下载文件 [英] JSP download file with ajax

查看:121
本文介绍了具有ajax的JSP下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要下载文件我正在使用ajax。我有三个状态:

To download file I am using ajax. I have three status:

"Download file" - before you click on the link.
"File is downloading..." - while the file is downloading
"Download file" - if the file was generated successful 
"Error while downloading file" - if there was any error while download the file.

我使用JSP,jquery ajax。如何确定使用ajax下载文件时出现错误或下载是成功的。

I'm using JSP, jquery ajax. How to determinate that there was an error with downloading the file with using ajax or the download was successfull.

推荐答案

您无法下载文件使用ajax,并使其有用。 Ajax响应加载到javascript内存中。 Javascript没有磁盘访问来保存该文件。

You cannot download a file using ajax, and make it useful. Ajax response is loaded into javascript memory. Javascript dont have disk access to save that file.

更好的做法是使用标题content-typeContent-Disposition:attachment;文件名=file name.ext使用正常请求。

So better do it in normal way using header "content-type" Content-Disposition: attachment; filename="file name.ext" using normal request.

这将下载文件和浏览器将为用户提供保存文件的选项。另请注意,这不会重新加载您的页面。

This will download file and browser will provide user an option to save file. Also note that this wont reload your page.

这篇关于具有ajax的JSP下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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