Не удалось отправить заявку - PullRequest
0 голосов
/ 17 февраля 2020

Я пытаюсь сделать запрос POST, преобразовать и загрузить файл в pdf, но у меня есть эта ошибка: documentmento.service.ts:

  download(documento: IDocumento) {
    return this.http.post(SERVER_API_URL + "api/download/", documento).subscribe((response) => {
      saveAs(response, documento.tituloDocumento + "." + "pdf");
    });
  }

DocumentResource

    @RequestMapping(value = "/download", method = RequestMethod.POST)
public void download(HttpServletResponse response, @RequestBody DocumentoDTO documentoDTO) throws IOException {
    File file = new File(documentoDTO.getPathorigdoc());
    response.setContentType("pdf");
    response.setHeader("Content-disposition", "attachment;" + documentoDTO.getTituloDocumento());
    response.getOutputStream().write(Files.readAllBytes(file.toPath()));
}

Access to XMLHttpRequest at 'https://autenticacao.campogrande.ms.gov.br/auth/realms/dev/protocol/openid-connect/auth?response_type=code&client_id=web_app&scope=openid%20offline_access%20profile%20email%20address%20phone%20roles%20web-origins%20jhipster&state=DooRYbdfmKCip2apn-NOTWQOqWBGkeh-AigMtoJVfcM%3D&redirect_uri=http://localhost:9000/login/oauth2/code/oidc' (redirected from 'http://localhost:9000/api/download/') from origin 'http://localhost:9000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
autenticacao.campogrande.ms.gov.br/auth/realms/dev/protocol/openid-connect/auth?response_type=code&client_id=web_app&scope=openid%20offline_access%20profile%20email%20address%20phone%20roles%20web-origins%20jhipster&state=DooRYbdfmKCip2apn-NOTWQOqWBGkeh-AigMtoJVfcM%3D&redirect_uri=http://localhost:9000/login/oauth2/code/oidc:1 Failed to load resource: net::ERR_FAILED
webpack-internal:///./node_modules/@angular/core/fesm5/core.js:6249 ERROR HttpErrorResponse

Ответы [ 2 ]

0 голосов
/ 17 февраля 2020

Возможность решить, но вы не управляете загрузкой, я полагаю, это потому, что или ответ возвращает ноль, мне нужно что-то исправить?

    @PostMapping(value = "/download")
public void download(HttpServletResponse response, @RequestBody DocumentoDTO documentoDTO) throws IOException {
    String caminho = "";

    caminho += "/arquivos/leisweb" + File.separator + documentoDTO.getPathorigdoc();

    File file = new File(caminho);
    response.setContentType("application/pdf");
    response.setHeader("Content-disposition", "attachment;" + documentoDTO.getTituloDocumento() + ".pdf");
    response.getOutputStream().write(Files.readAllBytes(file.toPath()));
}

  download(documento: IDocumento) {
    return this.http.post(SERVER_API_URL + "api/download", documento).subscribe((response) => {
      saveAs(response, documento.tituloDocumento + "." + "pdf");
    });
  }

Возврат: Выход: .web .rest.DocumentoResource.download () с результатом = null

0 голосов
/ 17 февраля 2020

Я не знаю, какой сервер вы используете, но вам нужно изменить значение заголовков в сервисе (пример в java)

Как обрабатывать CORS с помощью JAX-RS с трикотажем

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...