angular - 角度:如何从后端获取视频,在标头中发送 jwt 令牌
问题描述
希望你们都做得很好,我遇到了 Angular 的一个真正问题
后端向我发送一个对象,其中包含附件(图像或视频)的 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 (attachement.file) 在该请求的标头中发送 jwt 令牌(安全附件)。
我用安全管道做到这一点
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({
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)));
}
}
我的问题是所有这些仅适用于图像类型的文件附件,当它是视频时,请求总是失败
示例: 视频请求
我不知道类型视频,我必须用另一种方式或类似的东西,但我确定问题不是来自后端,因为我在邮递员中测试过,我总是得到视频
任何帮助,请
解决方案
一切看起来都很好。但在某些情况下blob.data
会给你实际的流。试试这个。
URL.createObjectURL(val.data) // assign val:any if neccessary
试试这个视频。如果这可行,您将不得不在您的管道中放置条件逻辑。
推荐阅读
- openmp - openmp taskwait 是否释放任务正在运行的核心?
- spring - 春季启动升级后阻塞:混合内容错误
- webhooks - Discord Webhook - “操作失败消息对此主机的请求过多。”
- r - 如何在R中合并具有不同名称的相同观察的不同数据框?
- javascript - 在 React 状态更新后触发异步请求的最佳方法?
- swift - Xcode - swift - 如何链接(嵌入)Cocoapods Lumberjack
- javascript - 使用 html、css 和 javascript 在点击时弹出每个复选框
- python-3.x - 滚动平均并聚合熊猫中的多列
- java - 返回可选值,取决于流内使用的方法抛出的异常
- java - java.sql.SQLException:没有合适的驱动程序 - COM.ibm.db2.jdbc.app.DB2Driver