Adr*_*ota 6 response httpclient angular angular5 angular6
我想在(角度 5)中制作一个这样的可观察对象。如何responseType在 Angular 6 中声明?
Angular 5 代码:
public ExportSomeThing(
ano: number
): Observable<IDownloadArquivo> {
let q = this.http
.get(this.apiUrl + ano + '/pordia/excel', {
responseType: ResponseContentType.Blob // <<<-- This code
}).map((res) => {
Run Code Online (Sandbox Code Playgroud)
我的角度 6 代码:
public MyExport(
ano: number
): Observable<IXpto> {
let q = this.http
.get( this.api_rul + ano + '/some_path/', {
responseType: "ResponseContentType.Blob" // <<<--- error here !!!
}).map((res) => {
Run Code Online (Sandbox Code Playgroud)
错误是:
[ts]
Argument of type '{ responseType: "ResponseContentType.Blob"; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
Types of property 'responseType' are incompatible.
Type '"ResponseContentType.Blob"' is not assignable to type '"json"'.
Run Code Online (Sandbox Code Playgroud)
我怎样才能http.get在 Angular 6 中制作类似的东西?
我正在尝试下载Excel方法!
解决方法 Angular 8
客户端:
getBlob(url: string): Observable<Blob> {
return this.http.get<Blob>(url, { observe: 'body', responseType: 'blob' as 'json' })
}
Run Code Online (Sandbox Code Playgroud)
注意“blob”作为“json”技巧
服务器端返回字节[]:
@GetMapping(value = "/api/someApi")
public @ResponseBody byte[] GetBlob() {
byte[] ret = this.service.getData();
return ret;
}
Run Code Online (Sandbox Code Playgroud)
这里给出了很好的解释。尝试下面的代码
public getRequest(urlPath: string): Observable<any> {
const options = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': token // if you have to give token
}),
// Ignore this part or if you want full response you have
// to explicitly give as 'boby'as http client by default give res.json()
observe:'response' as 'body',
// have to explicitly give as 'blob' or 'json'
responseType: 'blob' as 'blob'
};
// resObj should be of type Blob
return this.http.get(urlPath, options)
.map((resObj: Blob) => resObj)
.catch((errorObj: any) => Observable.throw(errorObj || 'Server error'));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14838 次 |
| 最近记录: |