简体   繁体   English

从 angular 中的请求正文中截取 FormData

[英]intercept FormData from request body in angular

send发送

let formData = new FormData();
http.post - - - - - - - - - - - formData;

get得到

request = { body: FormData }

I want to do in angular intercept我想在 angular 拦截

if(FormData's value instanceof File) {
    pass without intercept
}

how can I do it?我该怎么做?

or或者

request = { body : FileFormData(ex: FormData only for File) }

Use a HttpInterceptor使用 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);
  }
}

Then register the interceptor: app.module.ts然后注册拦截器:app.module.ts

providers: [
   { provide: HTTP_INTERCEPTORS, useClass: FormDataInterceptor, multi: true }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM