【问题标题】:Angular 6 open pdf from byte array sent from WebAPIAngular 6从WebAPI发送的字节数组打开pdf
【发布时间】:2018-10-19 08:53:44
【问题描述】:

您好,我在从 WebAPI 发送的字节数组打开 pdf 时遇到问题。

我的服务:

getPdfDocument(): Observable<any> {
        return this.httpClient
            .get(this.configuration.serverUrl + this.configuration.getPdfDoc, {
                responseType: "arraybuffer" //tried with 'blob'
            });
}

我的组件:

this.service.getPdfDocument()
        .subscribe(data => {
            var file = new Blob([data], { type: 'application/pdf' });   
            this.pdfContent = URL.createObjectURL(file);
            window.open(this.pdfContent);
        })

当我运行它时,我无法加载 PDF 文档...我启用了弹出窗口仍然没有乐趣...

【问题讨论】:

    标签: angular typescript google-chrome pdf httpclient


    【解决方案1】:

    试试这个:

    服务:

    getPdfDocument(): Observable<any> {
        let headers = new HttpHeaders({ 'Content-Type': 'application/JSON' });
        return this.httpClient
                   .get(this.configuration.serverUrl + this.configuration.getPdfDoc,
                        { headers: headers, responseType: 'blob' as 'json', observe: 'response' as 'body' }
                    });
            }
    

    请求:

    this.service.getPdfDocument()
            .subscribe(
                    (data) => {
                        this.openFileForPrint(data);
                    });
    
    openFileForPrint(data: HttpResponse<any>) {
            let fileUrl = window.URL.createObjectURL(data);
            window.open(fileUrl, '_blank', 'location=yes,height=600,width=800,scrollbars=yes,status=yes');
        }
    

    服务器端

    [HttpGet]
    public HttpResponseMessage getpdf(DateTime datum, int idlokacija)
    {
        var r = _printService.getdata(datum, idlokacija);
        if (r == null)
        {
            return new HttpResponseMessage(HttpStatusCode.NotFound);
        }
        return SendPdfFile(r);
    }
    
    public static HttpResponseMessage SendPdfFile(string filePath, bool brisanje = true)
    {
        var stream = new FileStream(filePath, FileMode.Open);
        HttpResponseMessage response = new FileHttpResponseMessage(filePath, brisanje)
        {
            StatusCode = HttpStatusCode.OK,
            Content = new StreamContent(stream)
        };
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        return response;
    }
    

    【讨论】:

    • 此实现抛出以下内容:无法在“URL”上执行“createObjectURL”:未找到与提供的签名匹配的函数。
    • @tom33pr 当您查看服务器响应时,您的响应标头如何?
    • 乔见下文: Access-Control-Allow-Credentials: true Access-Control-Allow-Headers: Content-Type Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS 访问-Control-Allow-Origin: * Cache-Control: no-cache Content-Length: 230962 Content-Type: application/json; charset=utf-8 日期:格林威治标准时间 2018 年 10 月 19 日星期五 11:05:35 过期:-1 编译指示:无缓存服务器:Microsoft-IIS/10.0 X-AspNet-Version:4.0.30319 X-Powered-By:ASP .NET X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcZGFlcmEtcHJ6ZXpkemllY2tpdFxTb3VyY2VcUmVwb3NcUmV2aWV3IG9mIERlY2lzaW9uczJcUm9EXFJvRFdTXGFwaVxHZXRUcmltRG9jdW1lbnQ=?=
    • 我的 WebApi 控制器返回字节数组:[HttpGet] [Route("api/GetDocument")] public byte[] Get()
    • @tom33pr 如果您可以在我编辑的答案中编辑代码的 api 端,您会发现我的 api 端的样子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2017-12-03
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多