[英]Image file upload with postman working, but my code is not working. is there any changes need to be done?
我正在嘗試以角度發布帶有表單數據的圖像文件。 在郵遞員中工作正常
在我設置的標題部分下的郵遞員中..
內容類型:多部分/表單數據,
令牌:(用戶令牌)
並且,在 body 中有兩個字段 askusImg:(選定的圖像文件),
反饋ID:144
當我發送 POST 請求時將 api_key 作為參數,圖像成功插入到 Postman 中,但在我的項目中出現錯誤。
<input type="file" id="ssImg" [(ngModel)]="scShots" [ngModelOptions]="{standalone: true}" (change)="fileChange($event)" class="form-control m-input" name="screenshot" multiple size="10" >
<br>
<div class="col-sm-12 form-group">
<button (click)="feedbackFormSubmit()" class="btn m-btn--pill btn-primary m-btn m-btn--custom" type="submit">Send</button>
</div>
imgFile;
fileChange(event) {
let fileList: FileList = event.target.files[0];
this.imgFile = fileList;
};
feedbackFormSubmit(){
let form_Data = new FormData();
let askUsID = 144;
let api = BlankVars.scShotAPI; //correct as checked
form_Data.append('askusImg', this.imgFile, this.imgFile.name);
form_Data.append('feedbackID', askUsID);
this._POST_api_Service.POST_ScreenShot(api, form_Data).subscribe(res => {
console.log("res", res);
})
}
POST_ScreenShot(api, postData) {
let localToken = JSON.parse(localStorage.getItem('currentUser')).token;
let api_key = 'Ij5DN44MDtnQv2I8zdAJg0xCGasLGJL6Jc6L4OOU';
let scShotUrl = GlobalVariable.BASE_API_URL + api + '?api_key=' + api_key;
let headersComCat = new Headers();
headersComCat.append('token', localToken);
headersComCat.append('Content-Type', 'multipart/form-data');
let optionsComCat = new RequestOptions({ headers: headersComCat });
return this.http.post(scShotUrl, postData, optionsComCat)
.map((res: Response) => res.json())
.catch((error: any) => {
if (error.status === 401) {
window.alert("Session Timeout. Please Login Again.");
//localStorage.clear();
this._router.navigate(['/logout']);
return Observable.throw(new Error(error.status));
}
});
}
當我檢查我的代碼時,localToken 和 API 密鑰也與我在郵遞員中傳遞的值相同。 但是我收到了這個錯誤。
您在需要流的地方提供了“未定義”。 您可以提供 Observable、Promise、Array 或 Iterable。
我可以知道需要更改什么嗎?
使用 Authorization 更改標頭“token”並將“Bearer”+ token 作為值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.