Скачивание файла по нажатию кнопки
Необходимо, чтобы при нажатии кнопки происходило скачивание файла.
Делал следующим образом. Это работает только если перейти по ссылке "localhost:8080/api/act/printform/21", а надо по нажатию кнопки.
Контролер
angular.module('app', []).controller('ActController', ['$scope' , 'ActService', function($scope, ActService) {
$scope.createAct = function () {
ActService.getPrintform(21);
};
}]);
Сервис
angular.module('app').service('ActService',function($http) {
this.getPrintform = function(id) {
query = "api/act/printform/" + id;
return $http.get(query);
};
});
REST
@GET
@Path("/printform/{id : \\d+}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getPrintForm(@PathParam("id") BigDecimal id) {
ActReport actReport = new ActReport();
ActDTO actDTO = new ActTransform().getDTO(actService.getById(id));
byte[] content = actReport.createAct(actDTO);
ResponseBuilder rb = Response.ok(content);
rb.header("Content-Disposition", "attachment; filename="
+ "reportBy" + actDTO.getId() +".pdf");
rb.header("Content-Length", content.length);
rb.header("Content-Type", "application/download");
return rb.build();
}
web.xml
<web-app>
<display-name>MavenSite</display-name>
<servlet>
<servlet-name>RestServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>ru.maven.rs</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
10
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Подскажите, как исправить.
Источник: Stack Overflow на русском