尝试在Angular 2中读取上传进度时Xhr请求未发送会话cookie

时间:2018-10-09 08:16:46

标签: angular file-upload session-cookies

我正在使用Angular2。我有一个上载模块,使用下面的代码可以很好地工作。我的系统将会话cookie发送到服务器并上传文件。

@Injectable()
export class CustomBrowserXhr extends BrowserXhr {

  constructor(private service:ProgressService) {
    super();
  }

  build(): any {
    let xhr = super.build();
    xhr.withCredentials = true;
    return <any>(xhr);
  }
}

最近我需要显示文件上传的进度,经过一些研究并遵循此线程Angular 2.0 How to Extend BrowserXhr to create progress bar?,我已经如下所示更新了CustomBrowserXhr。

build(): any {
    let xhr = super.build();
    xhr.withCredentials = true;

    xhr.upload.onprogress = (event) => {
      console.log("test");
    }; 

    return <any>(xhr);
  }

现在,当尝试上传文件时,浏览器不发送会话cookie 到服务器。我的文件上传代码如下。知道为什么会这样吗?

            let formData: FormData = new FormData();
            formData.append('uploadFile', file, file.name);
            let headers = new Headers();
            headers.append('Accept', 'application/json');
            let options = new RequestOptions({ headers: headers });
            this._http.post(this.url, formData, options)
                .map(res => res.json())
                .subscribe(
                data => {
                    this.fileLoadingFlag = false;
                    this.fileLoadedFlag = true;
                    this.fileName = data.fileName;
                    this.md5Hash = data.md5Hash;
                    this.fileNameValidator = false;
                    this.fileTypeValidator = true;
                },
                error => console.log(error)
                )

0 个答案:

没有答案