2017-10-10 66 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上運行郵遞員成功 –