// java
// 返回體是ResponseEntity<ByteArrayResource>
Path pathFile = Paths.get(path); // path為視頻文件
byte[] data = Files.readAllBytes(pathFile);
ByteArrayResource resource = new ByteArrayResource(data);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
return ResponseEntity.ok()
.headers(headers)
.contentLength(data.length)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
// 前端(模擬ajax請(qǐng)求,關(guān)鍵就是獲取請(qǐng)求體联四,如果不是bolb對(duì)象要轉(zhuǎn)成bolb對(duì)象,再用URL.createObjectURL生成url)
videoReady(url) {
let xhr = new XMLHttpRequest();
xhr.open('GET',url, true);
xhr.setRequestHeader('Accept', 'application/octet-stream');
xhr.setRequestHeader('token', author.token);
xhr.responseType = 'blob';
xhr.onload = function() {
if (this.status == 200) {
const blob = this.response;
let url = URL.createObjectURL(blob); //基于Blob對(duì)象生成一個(gè)URL
let videoElement = document.getElementById('myVideo');
videoElement.src = url;
} else {
console.error('文件出錯(cuò)');
}
};
xhr.onerror = function() {
console.error('請(qǐng)求出錯(cuò)!');
};
xhr.send();
}
// 組件被銷毀后
beforeDestroy() {
URL.revokeObjectURL(url)
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者