【发布时间】:2023-03-27 12:02:01
【问题描述】:
我想将文件(图像)保存到我的服务弹簧,但我有这个问题
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {FileUploader} from 'ng2-file-upload'; @Component({
selector: 'app-root',
templateUrl
./app.component.html','
styleUrls: ['./app.component.scss']
})
export class
here AppComponent implements OnInit {
@ViewChild('fileInput') fileInput: ElementRef;
uploader: FileUploader;
isDropOver: boolean; ngOnInit(): void {
const headers =
[{name: 'Accept', value: 'application/json'}];
this.uploader = new FileUploader({url:
'http://localhost:9090/api/auth/produits/files', autoUpload: true,
headers: headers});
this.uploader.onCompleteAll = () => alert('File uploaded');
}
fileOverAnother(e: any):
void {
this.isDropOver = e;
}
fileClicked() {
this.fileInput.nativeElement.click();
}
}
@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api/auth")
@PostMapping(value = "/produits/files")
@ResponseStatus(HttpStatus.OK)
public void handleFileUpload(@RequestParam("file") MultipartFile file)
throws IOException {
fileService.storeFile(file);
}
访问 XMLHttpRequest 在 'http://localhost:9090/api/auth/produits/files' 来自原点 'http://localhost:4200' 已被 CORS 策略阻止:响应 预检请求未通过访问控制检查: 响应中的“Access-Control-Allow-Origin”标头不能是 当请求的凭据模式为“包含”时,通配符“*”。这 XMLHttpRequest 发起的请求的凭证模式是
由 withCredentials 属性控制。
我不知道这段代码有什么问题。请帮帮我?
【问题讨论】:
-
允许的标题怎么样?
标签: angular spring-boot