首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角11向.Net核心后端请求pdf

角11向.Net核心后端请求pdf
EN

Stack Overflow用户
提问于 2020-11-20 07:56:40
回答 1查看 519关注 0票数 1

我想发送html,作为post请求的一部分到一个.NET核心网络api,转换为一个pdf在服务器上,并在客户端接收这个文件。我无法正确地获得请求/响应方法,因为我在客户机上得到了415或400个响应或解析错误。在角度上我

代码语言:javascript
复制
   this.apiService
      .generatePdf((this.diagram.nativeElement as HTMLDivElement).innerHTML)
      .subscribe((response) => {
        const url = window.URL.createObjectURL(new Blob([response]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', 'file.pdf');
        document.body.appendChild(link);
        link.click();
      });

generatePdf(html: string): Observable<any> {
    return this.http.post(
      `${this.urls.document}/html2pdf`,
      { html },
      {
        headers: new HttpHeaders().set('Content-Type', 'application/pdf')
      }
    );

在后端,方法是

代码语言:javascript
复制
[HttpPost("html2pdf")]
public FileResult Html2Pdf([FromBody] string html)
{
   HttpContext.Response.ContentType = "application/pdf";
   var stream = repository.Html2Pdf(html);
   return File(stream, "application/pdf");
}

编辑:我最近的努力导致了400个错误

代码语言:javascript
复制
this.apiService
      .generatePdf((this.diagram.nativeElement as HTMLDivElement).innerHTML)
      .subscribe(
        (blob) => {
          downloadFile(blob, 'diagram.pdf');
        }
          );
  generatePdf(html: string): Observable<Blob> {
    return this.http.post<Blob>(
      `${this.urls.document}/html2pdf`,
      { html },
      { responseType: 'blob' as 'json' }
    );
  }

在服务器上

代码语言:javascript
复制
[HttpPost("html2pdf")]
        public async Task<IActionResult> Html2Pdf([FromBody] string html)
        {            
            var bytes = await repository.Html2Pdf(html);
            return File(bytes, "application/pdf");
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-21 08:16:45

后端

代码语言:javascript
复制
[HttpPost("html2pdf")]
public async Task<IActionResult> Html2Pdf([FromBody] DocumentConvertRequest model)
{            
    var bytes = await repository.Html2Pdf(model.Html);
    return new FileContentResult(bytes, "application/pdf");
}

前部

代码语言:javascript
复制
generatePdf(html: string): Observable<any> {
  return this.http.post(
      `${this.urls.document}/html2pdf`,
      JSON.stringify({ html }),
      {
        headers: this.headers,
        responseType: 'arraybuffer'
      }
    );
  }

this.apiService
  .generatePdf((this.diagram.nativeElement as HTMLDivElement).innerHTML)
  .subscribe(
    (res) => {          
      const newBlob = new Blob([res], { type: 'application/pdf' });
      const data = window.URL.createObjectURL(newBlob);

      const link = document.createElement('a');
      link.href = data;
      link.download = 'diagram.pdf';
      link.dispatchEvent(
        new MouseEvent('click', {
          bubbles: true,
          cancelable: true,
          view: window
        })
      );

      setTimeout(function () {            
        window.URL.revokeObjectURL(data);
        link.remove();
      }, 0);
    }
  );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64925734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档