gpt4 book ai didi

html - Angular 6 - 从 httpClient 获取 csv 响应

转载 作者:搜寻专家 更新时间:2023-10-30 21:33:42 26 4
gpt4 key购买 nike

任何人都可以举一个从 Angular 6 httpClient 获取应用程序/八位字节流响应的示例。我正在使用下面的代码,但它不起作用(我收到未知错误 - 401 响应)-

import { saveAs } from 'file-saver';

getJobOutput() {
this.workflowService.fetchOutput(this.jobId,this.outputId).subscribe((response : any) => { // download file
var blob = new Blob([response.blob()], {type: 'application/octet-stream'});
var filename = 'file.csv';
saveAs(blob, filename);
});
}

服务如下-

 fetchOutput(jobId : string, outputId) {
var jobOutputURL = "myEnpoint";
var params = this.createHttpAuthorization(jobOutputURL,"GET");
params["format"] = "csv";
const options = {
headers: new HttpHeaders( { 'Content-Type': 'application/octet-stream',
'Accept' : 'application/octet-stream',
'Access-Control-Allow-Origin' : '*'}
)};

var endpoint = `${jobOutputURL}?oauth_consumer_key=${params["oauth_consumer_key"]}&oauth_signature_method=${params["oauth_signature_method"]}&oauth_nonce=${params["oauth_nonce"]}&oauth_timestamp=${params["oauth_timestamp"]}&oauth_version=1.0&format=${params["format"]}&oauth_signature=${params["oauth_signature"]}`;
return this.httpClient.get(endpoint, {...options, responseType: 'blob'});
}

最佳答案

要获取应用程序/八位字节流,您必须将 arraybuffer 设置为 Angular HttpHeaders 中的响应类型。

这是服务方法:

fetchOutput(): Observable<ArrayBuffer> {
let headers = new HttpHeaders();

const options: {
headers?: HttpHeaders;
observe?: 'body';
params?: HttpParams;
reportProgress?: boolean;
responseType: 'arraybuffer';
withCredentials?: boolean;
} = {
responseType: 'arraybuffer'
};

return this.httpClient
.get('https://your-service-url.com/api/v1/your-resource', options)
.pipe(
map((file: ArrayBuffer) => {
return file;
})
);
}

这是对服务方法和 saveAs 函数的调用:

this.yourService
.fetchOutput()
.subscribe((data: any) => {
const blob = new Blob([data], { type: 'application/octet-stream' });
const fileName = 'Your File Name.csv';
saveAs(blob, fileName);
})

正如其他用户的建议:401 Unauthorized 通常是由于缺少凭据而导致的客户端错误。

关于html - Angular 6 - 从 httpClient 获取 csv 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55221136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com