2017-10-10 41 views
1

发送图像,我想与http.post发送图像。我使用该代码块,但给我404错误。我尝试了邮差的API,返回成功。如何使用http.post

哪里是我的错吗?

谢谢。

changeprofileimg() 
    { 
    this.camera.getPicture({ 
     sourceType: this.camera.PictureSourceType.PHOTOLIBRARY, 
     destinationType: this.camera.DestinationType.DATA_URL, 
     quality: 100, 
     encodingType: this.camera.EncodingType.PNG, 
     targetHeight:500, 
     targetWidth:500, 
     correctOrientation:true 
    }).then(imageData => { 

     const image = 'data:image/jpeg;base64,'+imageData; 
     var veri; 
     var headers = new Headers(); 
     headers.append('Accept', 'application/json'); 
     headers.append('Authorization' , 'Bearer '+this.globalvars.getToken()); 
     headers.append('Content-Type', 'multipart/form-data'); 
     let options = new RequestOptions({ headers: headers }); 


     let postParams = { 
     file:image 
     } 

     this.http.post("http://.../api/profile/image/upload", postParams, options) 
     .subscribe(data => { 
      console.log(data['_body']); 
      veri=data['_body']; 
      veri = veri.replace(/\\/g, ""); 
      veri = JSON.parse(veri); 
      console.log(veri); 
     }); 

    }); 
    } 

回答

0

你应该使用FormData用于此目的:

 let formData = new FormData(); 
     formData.append('file', image); 


     this.http.post("http://.../api/profile/image/upload", formData, options) 
     .subscribe(data => { 
      console.log(data['_body']); 
      veri=data['_body']; 
      veri = veri.replace(/\\/g, ""); 
      veri = JSON.parse(veri); 
      console.log(veri); 
     }); 

    }); 

此外,由于你得到一个404错误,所以你应该仔细检查您的API端点和REST API的方法。

+0

我加入这个代码仍然返回“{”代码“:404,”状态“:”失败“‘消息’:‘文件加载错误’}” –

+0

最有可能是在您的API相关的终点一些问题,因为你'获取404 http状态。 – asmmahmud

+0

但这个API上运行邮递员成功 –