Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140390

Image file upload with postman working, but my code is not working. is there any changes need to be done?

$
0
0

I'm trying to post an image file with form data in angular. which is working fine in postman

In Postman under headers section I have set..

Content-Type : multipart/form-data,

token : (user token)

and, in body there are two fields askusImg : (selected image file),

feedbackID : 144

with api_key as params when I send POST request, the image is successfully inserting in Postman,but getting the error in my project.

HTML

<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>

TS

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

    }

PostService.ts

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

As I checked with my code localToken and API key also same as the values i passed in postman. But I'm getting this error.

error

You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.

May I know what needs to be changed?


Viewing all articles
Browse latest Browse all 140390

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>