我有一个 API 方法调用第三方 API 并为扩展名为 .gz 的文件发送二进制数据。我只是调用第三方 API 并将 HttpResponseMessage 发送回我的 Angular 代码。
下面是我的API服务代码
public async Task<HttpResponseMessage> DownloadGzippedFile(string segment, string folderPath, string fileName, string token)
{
using (var httpClient = new System.Net.Http.HttpClient())
{
// NOTE: accept all media types
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("*/*"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
System.Net.Http.HttpResponseMessage response;
try
{
var fileUrl = $"{uri}member/file/download/1.0?segment=" + segment + "&folderPath=" + folderPath + "&filename=" + fileName;
response = await httpClient.GetAsync(fileUrl);
string value = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
return response;
else
throw new HttpRequestException(response.ReasonPhrase);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
}
和 Angular 13 Type Script 代码
download(segment:string,folderPath:string,fileName:string) :Observable<Blob> {
folderPath='/'+folderPath.slice(0,folderPath.lastIndexOf('/'));
const url = 'http://localhost:22721/api/NseDownload/DownloadGzippedFile?segment='+segment+'&folderPath='+folderPath+'&fileName='+fileName+'&token='+localStorage.getItem("token");
return this.http.get(url,{ responseType: 'blob' });
}
download(node:any){
this.db.download('CM',node.folderPath,node.name).subscribe(response=>{
saveAs(response, node.name)
})
}
{"version":"1.1","content":{"headers":[{"key":"Content-Disposition","value":["attachment; filename=\"07765_C_member_missing_client_details.csv.gz\""]},{"key":"Expires","value":["Mon, 20 Feb 2023 10:19:19 GMT"]},{"key":"Content-Length","value":["39968"]}]},"statusCode":200,"reasonPhrase":"OK","headers":[{"key":"Vary","value":["Origin","Access-Control-Request-Method","Access-Control-Request-Headers","Origin","Access-Control-Request-Method","Access-Control-Request-Headers"]},{"key":"X-Frame-Options","value":["SAMEORIGIN"]},{"key":"X-XSS-Protection","value":["1; mode=block"]},{"key":"X-Forwarded-Proto","value":["https"]},{"key":"X-Content-Type","value":["nosniff"]},{"key":"Strict-Transport-Security","value":["max-age=31536000; includeSubdomains; preload"]},{"key":"Cache-Control","value":["no-store, no-cache, max-age=0"]},{"key":"Pragma","value":["no-cache"]},{"key":"Date","value":["Mon, 20 Feb 2023 10:19:19 GMT"]},{"key":"Connection","value":["keep-alive"]},{"key":"Set-Cookie","value":["HttpOnly; Secure"]}],"trailingHeaders":[],"requestMessage":{"version":"1.1","versionPolicy":0,"content":null,"method":{"method":"GET"},"requestUri":"{uri}/member/file/download/1.0?segment=CM&folderPath=/Reports&filename=07765_details.csv.gz","headers":[{"key":"Accept","value":["*/*"]},{"key":"Authorization","value":["Bearer eyJhbGciOiJSUzI1NiJ9.eyJtZW1iZXJDNSwiaWF0IjoxNjc2ODg4MzA1LCJqdGkiOiIzY2UzMGJjMy0yY2M4LTRjZDEtOWIyMy1jOWY0ZDEwMDgxOTgifQ.Dfn-G4US36sO3F1ekNhQZWk6J8uXH_PBaQGGGwevsNMncZgk4zUUJaC1peFqO9mCBy1UFmyO9ngw-YAP_aEzNg"]},{"key":"traceparent","value":["00-b76ec4bd3d634170ef8bce99e991ce57-76cf6e6800e6d661-00"]}],"properties":{},"options":{}},"isSuccessStatusCode":true}
你的 api 返回类型应该是
Task<IActionResult>.
然后你可以做:
return new FileStreamResult(await response.Content.ReadAsStreamAsync(), "application/gzip");