Angular WebApi从发布数据下载文件(FileResultStream)

时间:2019-07-10 07:11:37

标签: javascript .net file

尝试通过excel文件访问时出现以下错误,请下载此方法-使用邮递员可以正常工作。我还没有设法找到一种方法来完成这项工作-基本上,我正在将一个对象发布到API并返回一个excel,结果我需要下载客户端,但是返回的结果不是JSON设置return失败响应类型不正确。任何帮助将不胜感激。

 create(resource) {
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        responseType: 'arraybuffer',
        'Content-Disposition': 'attachment'
      })
    };


    console.log(this.url, httpOptions);
    this.http
      .post(this.url, JSON.stringify(resource), httpOptions)
      .subscribe(response =>
        console.log('done')
      );
  }

    console.log(this.url, httpOptions);
    return this.http
      .post(this.url, JSON.stringify(resource), httpOptions)
      .subscribe(response =>
        console.log('done')
      );

core.js:7187 ERROR 
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:51277/api/Reports/report", ok: false, …}
error:
error: SyntaxError: Unexpected token P in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:38886:51) at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:3240:31) at Object.onInvokeTask (http://localhost:4200/vendor.js:97394:33) at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:3239:60) at Zone.runTask (http://localhost:4200/polyfills.js:3017:47) at ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:3314:34) at invokeTask (http://localhost:4200/polyfills.js:4452:14) at XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:4489:21)
text: "PKm�Nk(Z�8/xl/workbook.xml �("
__proto__: Object
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://localhost:51277/api/Reports/report"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:51277/api/Reports/report"
__proto__: HttpResponseBase

API

    [System.Web.Http.HttpPost]
    [System.Web.Http.Route("Report")]
    public FileResultStream Report([FromBody] FormattedReportRequest config)
    {
        var result = Base(config);
        return result;
    }

FileResultStream类 公共任务ExecuteAsync(CancellationToken cancelledToken)         {

        var contentType = _fileName.IsNullOrWhiteSpace()
                          || _fileName == ".application" ?
            "application/x-ms-application" :
            MimeMapping.GetMimeMapping(Path.GetExtension(_fileName));

        var response = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(_data.ToArray())
        };

        response.Content.Headers.ContentLength = _data.Length;
        response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
        //response.Headers.AcceptRanges.Add("bytes");
        response.Content.Headers.ContentDisposition =
            new ContentDispositionHeaderValue("attachment")
            {
                FileName = _fileName
            };


        return Task.FromResult(response);
    }

0 个答案:

没有答案