Closed stephaneeybert closed 9 years ago
FYI, in my case setting withCredentials
to true did in fact add the credentials when uploading a file so it is the documented behavior.
@kyro38 But you also had to provide the credentials header ?
Nope, I just set the option to true. FYI, I'm using the v 1.0.3 of the module with AngularJS 1.2.26.
But at some point you have to provide some authentication payload. Like a base64 username password. How and when is this done then?
From MDN:
XMLHttpRequest.withCredentials Is a Boolean that indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies or authorization headers. The default is false. Note: This never affects same-site requests.
Yeap just had the same problem and i fixit by adding the auth token to the headers.
item.upload = new FileUploader({
url: '/api/uploads',
alias: 'attachment',
headers: { 'Authorization': 'Bearer ' + $cookieStore.get('token') },
domElement: item._id,
withCredentials:true,
formData: [
{document: $routeParams.id},
{concept: item._id}
]
});
When you make $http.post request it gets intercepted by $httpProvider.interceptors (in my case) and it adds the token to the header, but the post made by the Uploader does not get intercepted so no token is added. That appears to be case for me.
Im using the yeoman generator angular-full stack generator.
Ixibarra's method works for me but if you are using Angular's $cookies service you will need to parse the token because it is returned with extra dbl quotes. $cookies.get('token') = "123abc" JSON.parse($cookies.get('token')) = 123abc
In my case, it was enough to provide the authentication header as in:
new FileUploader({ url: ENV.NITRO_PROJECT_REST_URL + '/bts/upload', removeAfterUpload: false, headers: { 'Authorization': AuthService.getCredentialsHeaders() } });
At first I also had the withCredentials: true but it didn't help.