13

I have used this library for angular2 file upload https://github.com/valor-software/ng2-file-upload

Now I'm getting this error when I upload a file

XMLHttpRequest cannot load http://localhost:8080/files. Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:3000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Kokulan
  • 1,316
  • 3
  • 19
  • 35
  • this solved the same issue of mine https://stackoverflow.com/a/47334113/5236174 , please check if it is an acceptable answer in your case. – Lahar Shah Dec 11 '17 at 05:47

3 Answers3

29

Make withCredentials = false before uploading the item. You can put this code in ngOnInit/ constructor or ngOnChanges.

this.uploader.onBeforeUploadItem = (item) => {
  item.withCredentials = false;
}
Lahar Shah
  • 7,032
  • 4
  • 31
  • 39
1

Your server is responding with the following CORS Header

'Access-Control-Allow-Credentials' = true

This is a security that CORS provide, you are not allowed to do that. You cannot use Access-Control-Allow-Origin = * if you what to allow credentials. You will have to specify the exact domain. try Specifying

localhost:<portnumber>

For more information look at the following links

  1. Access-Control-Allow-Origin wildcard subdomains, ports and protocols
  2. Cross Origin Resource Sharing with Credentials
  3. CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true
Community
  • 1
  • 1
Ankesh
  • 4,847
  • 4
  • 38
  • 76
  • i have added "Access-Control-Allow-Origin", "*" this one – Kokulan Feb 17 '17 at 12:15
  • you cannot use it, in your header `Access-Control-Allow-Credentials` is coming as `true` which doesn't not allow you to use , `Access-Control-Allow-Origin` as `*`. Specify an exact origin `localhost:3000` or some how set `Access-Control-Allow-Credentials` as false – Ankesh Feb 17 '17 at 13:16
0

You can try this:

ngAfterViewInit() {
   this.uploader.onAfterAddingFile = (item => {
      item.withCredentials = false;
   });
}
Dino
  • 7,779
  • 12
  • 46
  • 85
El7or
  • 334
  • 1
  • 5
  • 18