ng2-file-upload icon indicating copy to clipboard operation
ng2-file-upload copied to clipboard

request faild

Open wengjincheng opened this issue 7 years ago • 4 comments

I use angualr2 and my backend is koa2,when i use ng2-file-upload,there is my TS code `import { Component } from '@angular/core'; import { UserService } from './server'; import {FileUploader} from "ng2-file-upload";

@Component({ moduleId: module.id, selector: 'my-edit', templateUrl: '../templates/editpage.html', styleUrls: ['../css/editPage.min.css'], providers: [UserService] })

export class EditPage { title:any; content:any; image:any; author:any; url:any = 'http://localhost:3005/api/createTodayFocus'; data:any = {}; closeResult: string; tip:any = ''; type:any = 0; constructor(private userService: UserService) { } submitAll(content){ this.data = {title:this.title,content:this.content,image:this.image,author:this.author,type:this.type}; this.userService.postNewsData(this.url,this.data) .subscribe( data =>{ console.log(data); this.tip = data; this.title = ''; this.content = ''; this.image = ''; this.author = ''; this.type = '0'; } ) }

public uploader:FileUploader = new FileUploader({
    url: "http://localhost:3005/api/uploadImage",
    method: "POST",
    itemAlias: "uploadedfile"
});


selectedFileOnChanged(event:any) {

    console.log(event.target.value);
    let value = event.target.value;
    this.image = value.split('fakepath\\');
    console.log(this.image[1]);
}

uploadFile() {
    for(let item of this.uploader.queue){
      console.log(item._file.name);
    }

    this.uploader.queue[0].onSuccess = function (response, status, headers) {
      
        if (status == 200) {
        
            let tempRes = JSON.parse(response);
            console.log(tempRes);
        } else {
        
            alert("error");
        }
    };
    this.uploader.queue[0].upload(); 
}

} and HTML code <input type="file" ng2FileSelect [uploader]="uploader" multiple /><br/> <p *ngFor="let item of uploader.queue"> {{item?.file?.name}} <button type="button" class="btn btn-success btn-xs" (click)="item.upload()" [disabled]="item.isReady || item.isUploading || item.isSuccess"> <span class="glyphicon glyphicon-upload"></span> Upload </button> </p>

when i upload image,return this XMLHttpRequest cannot load http://localhost:3005/api/uploadImage. Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. Origin 'http://localhost:4200' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

My backend have already fixed cors, so, why?

wengjincheng avatar Mar 28 '17 07:03 wengjincheng

try this

ngAfterViewInit() {
   this.uploader.onAfterAddingFile = (item => {
      item.withCredentials = false;
   });
}

credit : https://github.com/valor-software/ng2-file-upload/issues/399

Wizz14 avatar Mar 30 '17 07:03 Wizz14

thanks for @Wizz14

sambanthar avatar Dec 21 '17 09:12 sambanthar

thanks for @Wizz14 ...!!

anantdhas avatar Jun 26 '18 12:06 anantdhas

thanks @Wizz14 !!!

silviurdr avatar Nov 17 '20 19:11 silviurdr