简体   繁体   English

图像文件上传与邮递员工作,但我的代码不工作。 是否需要做任何改变?

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

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),并且,在 body 中有两个字段 askusImg:(选定的图像文件),

feedbackID: 144反馈ID: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.当我发送 POST 请求时将 api_key 作为参数,图像成功插入到 Postman 中,但在我的项目中出现错误。

HTML 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 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邮政服务.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.当我检查我的代码时,localToken 和 API 密钥也与我在邮递员中传递的值相同。 But I'm getting this error.但是我收到了这个错误。

error错误

You provided 'undefined' where a stream was expected.您在需要流的地方提供了“未定义”。 You can provide an Observable, Promise, Array, or Iterable.您可以提供 Observable、Promise、Array 或 Iterable。

May I know what needs to be changed?我可以知道需要更改什么吗?

change the header "token" with Authorization and put "Bearer " + token as value.使用 Authorization 更改标头“token”并将“Bearer”+ token 作为值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM