我的api代码返回一个文件,没问题:
[HttpGet("downloadProfilePic")]
public async Task<FileStreamResult> DownloadProfilePic()
{
try
{
var containerName = "profilepics";
string storageConnection = _config.GetSection("StorageConnectionString").Value;
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnection);
CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference((await _userService.GetUserGuid().ConfigureAwait(false)).ToString());
MemoryStream memoryStream = new MemoryStream();
await blockBlob.DownloadToStreamAsync(memoryStream).ConfigureAwait(false);
memoryStream.Position = 0;
return FileStreamResult(memoryStream, "image/png");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
我的角度服务没有正确接收:
downloadProfilePic(): Observable<any> {
return this.http.get<any>(this._url + '/downloadProfilePic');
}
错误拦截器正在触发,我不知道为什么:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(err => {
if (err.status === 401) {
// auto logout if 401 response returned from api
this.authenticationService.logout();
location.reload(true);
}
const error = err.error || err.statusText;
return throwError(error ? error : err);
}))
}
我在这里做错了什么?我想在浏览器中显示这个 memoryStream。
编辑:删除了角度服务代码中的 errorHandler。