问题描述
我是相当新的AJAX。我送使用AJAX请求到服务器。该服务返回的文本文件。但是,当返回的数据没有下载对话框出现。返回该文件中的其它服务如下:
I am fairly new to AJAX. I am sending a request to server using AJAX. The service returns a text file. But no download box appears when data is returned.The rest service that returns the file is as follows:
@Path("/examples")
public class ExampleCodesRest {
@POST
@Path("/getcode")
@Produces(MediaType.TEXT_PLAIN)
public Response getCodes(@Context ServletContext context){
String in=context.getRealPath("/WEB-INF/reports.jrxml");
File file=new File(in);
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition",
"attachment; filename=\"file_from_server.log\"");
return response.build();
}
}
我的AJAX调用如下:
My AJAX call is as follows:
$('a#link').click(function(event){
event.preventDefault();
$.ajax({
url: '/reports/rest/examples/getcode',
type: 'POST'
});
});
文件下载成功没有Ajax。随着AJAX,它不下载file.Please建议。
The file downloads successful without AJAX.With AJAX, it doesn't download the file.Please advice.
推荐答案
的建议很简单:你无法通过AJAX下载文件 - 这是一个安全策略。我的意思是,你可以下载数据,但不能将其保存到磁盘从JavaScript的一面。
Advice is simple: you cannot download files via AJAX - it's a security policy. I mean you can download the data, but you can't save it to disk from JavaScript side.
如果您想要下载的点击一个文件,那么你可以添加的href
你 A
标记。或用文件的网址
打开一个新窗口。
If you want to download a file on click, then you can just add href
to you a
tag. Or open a new window with file's URL
.
这篇关于Ajax调用下载文件从RESTful服务返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!