【问题标题】:How to intercept upload requests when using ng6-file-man?使用 ng6-file-man 时如何拦截上传请求?
【发布时间】:2019-08-23 03:39:02
【问题描述】:

我正在使用示例ng6-file-man-test 为我的应用程序创建文件上传功能,该示例使用ng6-file-man 模块来启用各种文件/文件夹操作功能。根据我的应用程序要求,我还使用拦截器来请求向它们添加令牌。拦截器适用于大多数请求,但不适用于上传和下载请求。

jwt.interceptor.ts

import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse }
  from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable()
export class JwtInterceptor implements HttpInterceptor {
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {


    const duplicate = request.clone({ params: request.params.set('token', 'jwt-token') });

    return next.handle(duplicate);
  }
}

我没有对示例中给出的 AppComponent 进行任何更改。

以下是添加了拦截器的app.module.ts。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';

import { AppComponent } from './app.component';
import { FileManagerModule } from 'ng6-file-man';
import { JwtInterceptor } from '../_helpers/jwt.interceptor';
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    HttpClientModule,
    BrowserModule,
    FileManagerModule

  ],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: JwtInterceptor,
      multi: true
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}

文件管理器组件如下

    export class FileManagerComponent implements OnInit, AfterContentChecked {
  tree: TreeModel;
  appLanguage = 'it';

  constructor() {
    let currentUser = JSON.parse(localStorage.getItem('currentUser'));
    const treeConfig: ConfigInterface = {
      baseURL: 'https://backend.com/',
      api: {
        listFile: 'api/file/list',
        uploadFile: 'api/file/upload?token=' + currentUser.access_token,
        downloadFile: 'api/file/download',
        deleteFile: 'api/file/remove',
        createFolder: 'api/file/directory',
        renameFile: 'api/file/rename',
        searchFiles: 'api/file/search'
      },
      options: {
        allowFolderDownload: false,
        showFilesInsideTree: true
      }
    };

    this.tree = new TreeModel(treeConfig);
  }

  itemClicked(event: any) {
    console.log(event);
  }

  ngAfterContentChecked() {
    console.log('after');
  }

  ngOnInit() {
    console.log('before');

    let container = document.querySelector('.file-manager-left');

  }

}

我想知道是否有可能的方法可以拦截模块向后端发出的uploaddownload 请求。

【问题讨论】:

    标签: angular angular-http-interceptors


    【解决方案1】:

    这应该是因为对于您拦截的所有请求,您都希望使用HttpService

    • Uplodad 正在使用 FineUploader 库,该库似乎与 HttpService 没有连接
    • 通过打开新窗口进行下载,其中插入的参数仅包含path.id

    如果您在我们的GitHub issues 上提出任何解决方案,我们会很高兴

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 1970-01-01
      • 1970-01-01
      • 2019-01-14
      • 2011-04-03
      • 2015-06-25
      • 1970-01-01
      • 2014-05-26
      • 2020-09-20
      相关资源
      最近更新 更多