在 angular 中拦截来自请求正文的 FormData
intercept FormData from request body in angular
发送
let formData = new FormData();
http.post - - - - - - - - - - - formData;
得到
request = { body: FormData }
我想在angular拦截
if(FormData's value instanceof File) {
pass without intercept
}
我该怎么做?
或
request = { body : FileFormData(ex: FormData only for File) }
使用 HttpInterceptor
import { Injectable } from '@angular/core';
import {
HttpEvent, HttpInterceptor, HttpHandler, HttpRequest
} from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class FormDataInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// FormData's value instanceof File, something along the lines of
if(req.body instanceof File) {
return next.handle(req);
}
// Do intended logic here
return next.handle(req);
}
}
然后注册拦截器:
app.module.ts
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: FormDataInterceptor, multi: true }
]
发送
let formData = new FormData();
http.post - - - - - - - - - - - formData;
得到
request = { body: FormData }
我想在angular拦截
if(FormData's value instanceof File) {
pass without intercept
}
我该怎么做?
或
request = { body : FileFormData(ex: FormData only for File) }
使用 HttpInterceptor
import { Injectable } from '@angular/core';
import {
HttpEvent, HttpInterceptor, HttpHandler, HttpRequest
} from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class FormDataInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// FormData's value instanceof File, something along the lines of
if(req.body instanceof File) {
return next.handle(req);
}
// Do intended logic here
return next.handle(req);
}
}
然后注册拦截器: app.module.ts
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: FormDataInterceptor, multi: true }
]