Angular:如何下载文件而不在 url 中显示 access_token
Angular: how to download file without showing access_token in url
我的 Web 应用程序受 Oauth2 保护。对于 ajax 调用,必须在请求文件夹中提供 access_token。例如,这里是 factory.js 中的一种方法:
service.getAll = function () {
var url = SERVER + "/ristore/foundation/";
return $http({
headers: {'Authorization': 'Bearer ' + $window.localStorage.getItem("access_token")},
url: url,
method: 'GET',
crossOrigin: true
})
}
现在我想从网页上下载一个文件。文件通过流从服务器传递到客户端:
@RequestMapping(
value = "/ristore/foundation/xml/{filename}",
method = RequestMethod.GET,
produces = "application/xml")
public ResponseEntity<byte[]> downloadXMLFile(@PathVariable String filename) throws IOException {
FileSystemResource xmlFile = new FileSystemResource("/rsrch1/rists/moonshot/data/prod/foundation/xml/" + filename + ".xml");
byte [] content = new byte[(int)xmlFile.contentLength()];
IOUtils.read(xmlFile.getInputStream(), content);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream"))
.contentLength(xmlFile.contentLength())
.body(content);
}
在html中,我在点击开始下载的按钮上指定了ng-click():
<td data-title="'File'" class="text-center"><span class="glyphicon glyphicon-download-alt" ng-click="download(report.filename)"></span></a>
</td>
根据这个 post 的答案,$window.open
用于处理我的控制器中的 url:
$scope.download = function(filename) {
var url = "http://rcdrljboss01a:9880/ristoreService/ristore/foundation/xml/" + filename + "?access_token=" + $window.localStorage.getItem("access_token");
$window.open(url);
}
我可以通过这种方式下载文件,但是下载时显示access_token url。有没有办法在 url 中隐藏 access_token?
您应该将访问令牌放在 header 而不是查询参数中。
此 tutorial 详细说明了其工作原理。
编辑:有没有办法将令牌添加到 wondow.open(..) 的 header?
不行,好像不能直接把header换成window.open(..)
你能做什么:
用ajax获取xml,打开一个新的window并将文件设置为window的内容(伪代码,可能需要转换内容到字符串):
var win = open('some-url','windowName','height=300,width=300');
win.document.write(content);
我的 Web 应用程序受 Oauth2 保护。对于 ajax 调用,必须在请求文件夹中提供 access_token。例如,这里是 factory.js 中的一种方法:
service.getAll = function () {
var url = SERVER + "/ristore/foundation/";
return $http({
headers: {'Authorization': 'Bearer ' + $window.localStorage.getItem("access_token")},
url: url,
method: 'GET',
crossOrigin: true
})
}
现在我想从网页上下载一个文件。文件通过流从服务器传递到客户端:
@RequestMapping(
value = "/ristore/foundation/xml/{filename}",
method = RequestMethod.GET,
produces = "application/xml")
public ResponseEntity<byte[]> downloadXMLFile(@PathVariable String filename) throws IOException {
FileSystemResource xmlFile = new FileSystemResource("/rsrch1/rists/moonshot/data/prod/foundation/xml/" + filename + ".xml");
byte [] content = new byte[(int)xmlFile.contentLength()];
IOUtils.read(xmlFile.getInputStream(), content);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream"))
.contentLength(xmlFile.contentLength())
.body(content);
}
在html中,我在点击开始下载的按钮上指定了ng-click():
<td data-title="'File'" class="text-center"><span class="glyphicon glyphicon-download-alt" ng-click="download(report.filename)"></span></a>
</td>
根据这个 post $window.open
用于处理我的控制器中的 url:
$scope.download = function(filename) {
var url = "http://rcdrljboss01a:9880/ristoreService/ristore/foundation/xml/" + filename + "?access_token=" + $window.localStorage.getItem("access_token");
$window.open(url);
}
我可以通过这种方式下载文件,但是下载时显示access_token url。有没有办法在 url 中隐藏 access_token?
您应该将访问令牌放在 header 而不是查询参数中。
此 tutorial 详细说明了其工作原理。
编辑:有没有办法将令牌添加到 wondow.open(..) 的 header?
不行,好像不能直接把header换成window.open(..)
你能做什么:
用ajax获取xml,打开一个新的window并将文件设置为window的内容(伪代码,可能需要转换内容到字符串):
var win = open('some-url','windowName','height=300,width=300');
win.document.write(content);