Kokulan
Kokulan

Reputation: 1364

Photo not uploading to backend

I have created the back-end to get the file using the post method it's working on Postman.

but from angular2 front-end it's not working this is the service code I have done.

data1 is the image data

addData(postData: Image, data1:any): Observable<Team[]> {
    console.log("team service");   
        this.image= postData;
        let body = this.image;


        let headers2= new Headers({ 'Content-Type': 'multipart/form-data' });
        let options = new RequestOptions({ headers: headers });


        return this.http.post(this.actionUrl, data1, options)
                .map(this.extractData)
                .catch(this.handleError);       
    }

it's giving error:

POST http://localhost:8080/user/45646/userName/userPhoto 500 (Internal Server Error) 500 - Internal Server Error

Upvotes: 1

Views: 33

Answers (1)

Kim
Kim

Reputation: 1010

This is the way i use to post my data via RestAPI and it's working...

Wish below sample code is helpful for you..

private httpCreate(data: T): Promise<T> {
    let headers = new Headers({'Content-Type': 'application/json'})
    return this.http.post(this.getUrl(), JSON.stringify(data), {headers: headers}).toPromise().then(res => res.json()).catch(this.handleError)
}

Upvotes: 1

Related Questions