Server.component.html
<label for="user_data">Upload file</label>
<input type="file" multiple formControlName="file" class="form-control" id="file" (change)=uploadFile($event) accept=".pdf,.docx" required>
Server.component.ts
uploadFile(event) {
let Sport_files = event.target.files;
if (Sport_files > 0) {
console.log(this.sports_videoForm.value.file); // You will see the file link
this.dataService.uploadFile(this.sports_videoForm.value.file);
}
SportsData.service.ts(服务组件实现)
uploadFile(file) {
let formData: FormData = new FormData();
formData.append('file', file, file.name);
this.http.post("http://localhost:/4200/", formData)
}
在“培训”部分中获取预期课程的文件sports_training.component.html
<a href="{{item.file}}" target="_blank" class="col-sm-4" style="padding-bottom: 10px">uploaded file</a>
答案 0 :(得分:0)
您需要使用DomSanitizer的bypassSecurityTrustUrl
方法来使角度信任该URL
您可以在服务中创建一个通用方法,以在整个应用程序中使用,因为您可能需要在应用程序的多个组件中使用它。
common.service.ts:
import { DomSanitizer } from '@angular/platform-browser';
...
constructor(private domSanitizer: DomSanitizer) {}
sanitize(url: string){
return this.sanitizer.bypassSecurityTrustUrl(url);
}
sports_training.component.html:
<a [href]="commonService.sanitize(item.file)" target="_blank" class="col-sm-4" style="padding-bottom: 10px">uploaded file</a>
您也可以在sports_training组件中创建sanitize
方法,但是我建议您使用通用服务