javascript - 如何添加标题以形成 Angular 5中的数据

标签 javascript angular typescript header

如何添加标题以形成具有进度条的数据?

我收到以下错误:

ERROR in src/app/services/auth.service.ts(91,23): error TS2554: Expected 2-4 arguments, but got 5.

代码:

   public upload(
    files: Set<File>
  ): { [key: string]: { progress: Observable<number> } } {
    // this will be the our resulting map
    const status: { [key: string]: { progress: Observable<number> } } = {};

    files.forEach(file => {
      // create a new multipart-form for every file

      const formData: FormData = new FormData();
      formData.append('file', file, file.name);

      // formData.append('name', course, course.name);
      // formData.append('text', username, username.name);
      let headers = new Headers();
      this.loadToken();
      headers.append('Authorization', this.authToken);
      // headers.append('Content-type', undefined);


      // create a http-post request and pass the form
      // tell it to report the upload progress

      const req = new HttpRequest('POST', 'users/upload', formData,{headers: headers},{
        reportProgress: true
      });

      // create a new progress-subject for every file
      const progress = new Subject<any>();

      // send the http-request and subscribe for progress-updates

      const startTime = new Date().getTime();
      this.https.request(req).subscribe(event => {
        if (event.type === HttpEventType.UploadProgress) {
          // calculate the progress percentage

          const percentDone = Math.round((100 * event.loaded) / event.total);
          // pass the percentage into the progress-stream
          progress.next(percentDone);
        } else if (event instanceof HttpResponse) {
          // Close the progress-stream if we get an answer form the API
          // The upload is complete
          progress.complete();
        }
      });

      // Save every progress-observable in a map of all observables
      status[file.name] = {
        progress: progress.asObservable()
      };
    });

    // return the map of progress.observables
    return status;
  }

最佳答案

ERROR in src/app/services/auth.service.ts(91,23): error TS2554: Expected 2-4 arguments, but got 5.

错误消息显示它需要 2-4 个参数,但得到了 5 个。

headersreportProgress 不应该是单独的参数,它们都应该是 HttpRequest 中第四个参数的一部分.

要修复该错误,请更改 HttpRequest,如下所示:

const req = new HttpRequest('POST', 'users/upload', formData,
  { headers: headers, reportProgress: true });

关于javascript - 如何添加标题以形成 Angular 5中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58423357/

相关文章:

javascript - 使用 self.location 在新选项卡中打开页面?

javascript - 从 2.7.x 升级到 2.8.x 后出现 TypeScript 错误 TS2349

javascript - 为什么 TypeScript 不封装私有(private)字段?

javascript - 如何更新/重置formBuilder属性值?

javascript - 使用 Web API 时,jQuery $.ajax() 方法未正确设置 contentType

javascript - 无法在函数中设置未定义的属性

javascript - 如何将默认占位符文本提供给 ckeditor 中的文本区域?

angular - 响应式(Reactive)开发 | RxJS 异步管道不工作

forms - 手动设置 FormBuilder 控件的值

javascript - 正确的 ES6 导入以添加力矩精确范围插件