C# 下载FileResult不使用Angular
我正在尝试下载文件(pdf、word、excel),它以字节[]的形式保存在DB中,下面是我的代码。但是在调用该方法时出现错误(意外的令牌错误)。 请引导我去修理它 控制器代码:C# 下载FileResult不使用Angular,c#,.net,angular,asp.net-mvc,C#,.net,Angular,Asp.net Mvc,我正在尝试下载文件(pdf、word、excel),它以字节[]的形式保存在DB中,下面是我的代码。但是在调用该方法时出现错误(意外的令牌错误)。 请引导我去修理它 控制器代码: [HttpPost] public async Task<FileResult> AttachmentById([FromBody]ReviewAttachmentModel attachmentRequest) { ReviewAttachmentM
[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;
}
}
错误:
更改此选项:
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 });
}
公共下载ReviewAttachment(reviewAttachmentModel:any):可观察{
此._urlserviceDetails=this.baseHref+“/ReviewProfile/AttachmentById”;
const headers:HttpHeaders=新的HttpHeaders();
headers.append('Content-Type','application/octet-stream');
返回this.http.post(this._urlserviveillanceDetails,reviewAttachmentModel,{headers:headers});
}
为此:
public downloadReviewAttachment(reviewAttachmentModel: any): Observable<blob> {
this._urlSurveillanceDetails = this.baseHref + "/ReviewProfile/AttachmentById";
return this.http.post(this._urlSurveillanceDetails, reviewAttachmentModel, { responseType: 'blob'});
}
公共下载ReviewAttachment(reviewAttachmentModel:any):可观察{
此._urlserviceDetails=this.baseHref+“/ReviewProfile/AttachmentById”;
返回this.http.post(this._urlserviveillancedetails,reviewAttachmentModel,{responseType:'blob'});
}
通过删除泛型参数并添加所需的responseType。Angular知道必须做什么。检查这是否有帮助@gkulshrestha-感谢它的帮助…通过在标题中添加适当的响应来解决问题。请查看我的答案,我相信它简化了问题code@StPaulis-谢谢,这对我有帮助
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'});
}