1

I'm trying to upload some files to an Express server, the server is using the cors() module like this

app.use(cors());

this is the Angular 2 code to upload the files

let formData:FormData = new FormData();
    for(let i = 0; i < files.length; i++) {
        let file = files[i];
        formData.append(`file`+(i+1), file, file.name);
    }

    let token = localStorage.getItem('supersecrettoken');
    let headers = new Headers();
    headers.append("Content-Type", 'multipart/form-data');
    headers.append("auth", token);

    return this.http.post(`${Constants.SERVER_IP}/imports`, formData, {headers: headers})
        .map(response => {
            return response.json();
        })
        .catch(this.logger.handleError).share();

Now this returns the following error

 No 'Access-Control-Allow-Origin' header is present on the requested resource

If I change the multipart/form-data to application/json it will work.

How can I upload multiple files to the server then?

Regards!

troyz
  • 1,345
  • 2
  • 11
  • 19
  • What CORS headers ave you set in the server? – Suraj Rao Mar 03 '17 at 10:45
  • I've tried using the cors module (It works for the rest of the app, but won't accept multipart/form-data) and these ones Access-Control-Allow-Origin", "*" "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept" – troyz Mar 03 '17 at 10:47
  • Are you able to get response headers? – Suraj Rao Mar 03 '17 at 10:50
  • Sorry for my ignorance, but how can I check that? – troyz Mar 03 '17 at 10:51
  • are you using web developer tools? chrome developer tools has network tab which enabled give you the request and response headers sent/received. https://developers.google.com/web/tools/chrome-devtools/network-performance/ – Suraj Rao Mar 03 '17 at 10:54
  • http://stackoverflow.com/questions/4423061/view-http-headers-in-google-chrome – Suraj Rao Mar 03 '17 at 10:57
  • Oh right, thanks. I'm getting this https://gyazo.com/6912f74f230d939196319c856206e2c6 – troyz Mar 03 '17 at 11:01
  • thats 403 error code..different error – Suraj Rao Mar 03 '17 at 11:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137140/discussion-between-troyz-and-suraj). – troyz Mar 03 '17 at 11:08

1 Answers1

1

You probably solved this by now but if not, just add this to the headers:

headers.append('Access-Control-Allow-Origin', '*');

The '*' means any domain. You might want to restrict it to specific domain(s).

Will
  • 1,080
  • 12
  • 13