[英]ANGULAR : how to get a video from the backend sending jwt token in headers
希望你们都做得很好,我遇到了 Angular 的真正问题
后端给我发一个 object 包含一个 Arrays 的 url 附件(图像或视频):
attachments:[{
id: 4
title: "d439e68f-ece6-4d80-a0b7-111fb337a8e6.jpeg"
content: null
file: "http://api-coldplace-dev.piman-analytics.fr/api/tutorial/4/attachments/4"
type: "image"
mime: "image/jpeg"
duration: 0
order: 0
cover: 0
external_link: 0
},{
id: 5
title: "file_example_MP4_480_1_5MG.mp4"
content: null
file: "http://api-coldplace-dev.piman-analytics.fr/api/tutorial/4/attachments/5"
type: "video"
mime: "video/mp4"
duration: 31
order: 1
cover: 0
external_link: 0
}]
要获得该附件,您必须使用另一个 http 请求 GET 到 url (附件.file )在该请求的附件中发送 ZCFD61B8A7397FA7C10B2AE548F5BFAFZ 令牌(安全)。
我用安全的 pipe 做到这一点
HTML:
<img *ngIf="file?.type?.toString()?.includes('image')"
[src]="file?.source | secure | async "
height="70px"
width="70px"/>
<video *ngIf="file?.type?.toString()?.includes('video')"
height="70px"
width="70px"
[src]="file?.source | secure | async"
(loadedmetadata)="getDuration($event, i)">
PIPE:
@Pipe({
name: 'secure'
})
export class SecurePipe implements PipeTransform {
constructor(private http: HttpClient, private sanitizer: DomSanitizer) { }
transform(url): Observable<SafeUrl> {
return this.http
.get(url, { responseType: 'blob' })
.map(val => this.sanitizer.bypassSecurityTrustUrl(URL.createObjectURL(val)));
}
}
我的问题是所有这些仅适用于图像类型的文件附件,当它是视频时,请求总是失败
示例:视频请求
我不知道类型视频,我必须用其他方式或类似的东西,但我确定问题不是来自后端,因为我在 postman 测试,我总是得到视频
任何帮助,请
一切看起来都很好。 但在某些情况下, blob.data
会为您提供实际的 stream。 试试这个。
URL.createObjectURL(val.data) // assign val:any if neccessary
试试这个视频。 如果这可行,您必须在 pipe 中放入条件逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.