2016-09-23 56 views
0

我已经创建了后端,使用它在Postman上工作的Post方法获取文件。照片不上传到后端

但从angular2前端它不工作这是我所做的服务代码。

data1是图像数据

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);  
    } 

它给错误:

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

回答

0

这是我使用通过RESTAPI后我的数据的方式,它的工作...

愿望下面的示例代码对您有所帮助..

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) 
} 
+0

它适用于json数据。我想使用'form-data'内容类型来传递文件 – kohli