【问题标题】:Download FileResult not working with Angular下载 FileResult 不适用于 Angular
【发布时间】:2020-09-22 15:29:12
【问题描述】:

我正在尝试下载在 DB 中保存为 bytes[] 的文件(pdf、word、excel),下面是我的代码。但是在调用方法时出现错误(意外的令牌错误)。 请指导我修复它。

控制器代码:

[HttpPost]
        public async Task<FileResult> AttachmentById([FromBody]ReviewAttachmentModel attachmentRequest)
        {
            ReviewAttachmentModel model = new ReviewAttachmentModel();
            try
            {
                ReviewAttachmentDto reviewAttachmentDto = new ReviewAttachmentDto
                {
                    DocumentKey = attachmentRequest.DocumentKey,
                    ReviewKey = attachmentRequest.ReviewKey,
                    UserId = attachmentRequest.UserId,
                };
                ReviewAttachmentDto _attachmentDto = reviewAttachmentDto;

                string requestBody = JsonConvert.SerializeObject(_attachmentDto);

                //Call API
                string _responseObj = await WebAPIHelper.PostDataToAPI(appSettings.ReviewAttachmentUrl, requestBody, this.loggedinUser.CookieCollection, accessToken);
                model = JsonConvert.DeserializeObject<ReviewAttachmentModel>(_responseObj);

               //  model.Document - byte[]
                return File(model.Document, model.DocumentType, model.DocumentName);

            }
            catch (Exception ex)
            {
                return null;
            }
        }

Service.ts:

public downloadReviewAttachment(reviewAttachmentModel: any): Observable<any> {
    this._urlSurveillanceDetails = this.baseHref + "/ReviewProfile/AttachmentById";
    const headers: HttpHeaders = new HttpHeaders();
    headers.append('Content-Type', 'application/octet-stream');
    return this.http.post<any>(this._urlSurveillanceDetails, reviewAttachmentModel, { headers: headers });
  }

组件.ts:

onAttachmentDownload(documentKey: any, reviewKey: any) {
      let reviewAttachmentModel: any = {
        documentKey: documentKey,
        reviewKey: reviewKey
      };

    this._surveillanceService.downloadReviewAttachment(reviewAttachmentModel).subscribe(data => {
      if (data != undefined) {
      }
    })
  }

错误:

【问题讨论】:

  • 检查这是否有帮助 stackoverflow.com/questions/51682514/…
  • @gkulshrestha - 感谢它的帮助......它通过在标题中添加适当的响应来解决。
  • 请查看我的回答,我相信它简化了代码

标签: c# .net angular asp.net-mvc


【解决方案1】:

改变这个:

public downloadReviewAttachment(reviewAttachmentModel: any): Observable<any> {
    this._urlSurveillanceDetails = this.baseHref + "/ReviewProfile/AttachmentById";
    const headers: HttpHeaders = new HttpHeaders();
    headers.append('Content-Type', 'application/octet-stream');
    return this.http.post<any>(this._urlSurveillanceDetails, reviewAttachmentModel, { headers: headers });
  }

到这里:

public downloadReviewAttachment(reviewAttachmentModel: any): Observable<blob> {
        this._urlSurveillanceDetails = this.baseHref + "/ReviewProfile/AttachmentById";
        return this.http.post(this._urlSurveillanceDetails, reviewAttachmentModel, { responseType: 'blob'});
      }

通过删除通用参数并添加所需的 responseType。 Angular 知道它必须做什么。

【讨论】:

    猜你喜欢
    • 2018-04-19
    • 2019-04-05
    • 2013-08-18
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多