Код стороны Java-сервера:
public static void forceDownloadExcel(HttpServletResponse response, Workbook content,
String fileName) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename="
+ fileName + ".xlsx");
ServletOutputStream out = null;
try {
out = response.getOutputStream();
content.write(out);
out.flush();
} catch (Exception e) {
logger.error(String.format("error while downloading %s.xlsx",fileName), e);
} finally {
try {
out.close();
} catch (IOException e) {
logger.error("error closing output stream",e);
}
}
}
JS-контроллер:
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
$scope.downloadFile= function(data, headers) {
var fileName='test.xlsx';
var contentType=headers['content-type'];
var blob = new Blob([data], {
type: contentType
});
var url = URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
//hide spinner here
window.URL.revokeObjectURL(url);
}
$scope.downloadLineitems = function() {
Dios.get({dios:'dios',third:'download',fourth:'lineitemexcel',fifth:
$routeParams.id},function(data, responseHeaders){
$scope.downloadFile(data,responseHeaders());
});
Служба JS:
.factory('Dios', ['$resource','$rootScope',
function($resource, $rootScope){
return $resource($rootScope.urlBase+'/dios/:id/:status/:third/:fourth/:fifth/:pageNo', {}, {
get: {
method: 'GET'
},
update: {
method: 'PUT'
}
});
}])
, используя приведенный выше код, я могу загрузить пустой файл.Если эта строка кода ниже, я получаю загруженный правильный файл
$window.location.href = $rootScope.urlBase +'/dios/download/lineitemexcel/'+$routeParams.id;
Но мне нужно сделать это с помощью кода JS без использования $window.location.href
, где изменение требуется в JAVAсторона сервера или сторона JS.