小编Mad*_*dhu的帖子

下载PDF文件,Angular 6和Web API

我想使用Angular 6和Web API下载PDF.这是代码实现,

mycomponent.ts

download(myObj: any) {
    this.testService.downloadDoc(myObj.id).subscribe(result => {

        var url = window.URL.createObjectURL(result);
        window.open(url);
        console.log("download result ", result);
    });
}
Run Code Online (Sandbox Code Playgroud)

myService.ts

downloadDoc(Id: string): Observable<any> {
    let url = this.apiUrl + "api/myApi/download/" + Id;
    return this.http.get(url, { responseType: "blob" });
}
Run Code Online (Sandbox Code Playgroud)

Web API服务

[HttpGet("download/{DocId}")]
    public async Task<HttpResponseMessage> GetDocument(string docId)
    {
        var docDetails = await _hoaDocs.GetDocumentDetails(docId).ConfigureAwait(false);
        var dataBytes = docDetails.Stream;
        var dataStream = new MemoryStream(dataBytes);

        var response = new HttpResponseMessage
        {
            StatusCode = HttpStatusCode.OK,
            Content = new StreamContent(dataStream)
        };

        response.Content.Headers.ContentDisposition = …
Run Code Online (Sandbox Code Playgroud)

asp.net-core-webapi angular angular6

11
推荐指数
2
解决办法
2万
查看次数

标签 统计

angular ×1

angular6 ×1

asp.net-core-webapi ×1