我正在按照本指南返回视频下载响应:https ://laravel.com/docs/5.8/responses#file-downloads
我想通过 API 调用来做到这一点,其中用户是通过不记名令牌来识别的。
ajax 调用应该是什么样子,所以我在将它“注入”到 HTML5 视频 src 的同时得到这个响应?
我已经尝试过以下
控制器
$path = storage_path('app/videos/'.$request->input('code').'.mp4');
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
Javascript
$.ajax({
type: 'GET',
url: '/api/user/video?code=2y101QBU5kxvPpaqGFFI16ZPeWg6jxXOQtwnTBvM61OjgrPACE2VA2y100oygu8kajpnp9i6U6ug28oC0dNnFxd5rNxvZeupBVedbzFgPo62i',
headers: {
"Authorization":"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjZlY2NkYzI4NzZlYmFiMjFhNTA5NTZiZGFkMGI4Mjk0ZjM2NTExOWM1YTU1NjI5MzQ5Y2QzY2QxMmZkZWM4NDE1YTEyYjUzMzFlMDA4OTEwIn0.eyJhdWQiOiIxIiwianRpIjoiNmVjY2RjMjg3NmViYWIyMWE1MDk1NmJkYWQwYjgyOTRmMzY1MTE5YzVhNTU2MjkzNDljZDNjZDEyZmRlYzg0MTVhMTJiNTMzMWUwMDg5MTAiLCJpYXQiOjE1Njc0OTU1NTgsIm5iZiI6MTU2NzQ5NTU1OCwiZXhwIjoxNTk5MTE3OTU4LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.MSxiEo-UswoKS5XqfezR4MDDgUvnsQuSwbM_IawIpERqNncY4F51H_mJ-chrIuA9cKcurj79Qpop2MNxyGTVSmT07DYd9jApXV3ayMbCmLLLZMJMIDsXP7_g9v-0ogBc-jYPkVFuXdzrxtmuOfi6XwUPDKq9JEhl9sB3tpqW2x3rUbPGE--vQvToXBMia8l9y5-2er6M2Rrbz0fit912fi_5LxjOkbm2MRCwhoFIhqPR_2VdGH733CmpY_pyIQs6XPbssIt7PssOryvreaQZUsq1XrUk82QjuZpDELufWnr1F2foc9UeLQaGthIDY4RNNR1nd4n7ZuG-FrKfnzC3eq2nfF9wpG3o8xMHoaOMYl1bbZJkE41BMPagwIv8F8KJwSJZpjxrq9LgTWs9-WVUbBFnf85ieER5-ltKM5vC8uwTYcETLxQzEBC_qf-durUC07740qf5r-oIlAfVQmksZgrG7XgtAkZqcZEEeiVsWE_JOcFiv0lj4IOcX6NhZR2RDP_reTTauOxxqt56VX0weib3F0Ub1DKe2ATbDBG9m7bCIRSIYUf58HXthHmjQKJ4ANxYxSwV3gcJsOodUsZTg9e5sjwGwBtIXrNybrFswQlE76kGyQ4-L1R6XTHFmKIWS0ac-Xk4RAmWoBZxDn-RNw0YAfsGtzj4XimJKRx36dM",
}
}).done(function(data) {
var video = $('#myvideo')[0];
video.src = data;
video.load();
video.play();
});
但我在控制台中收到以下错误
(index):106 Uncaught (in promise) DOMException
(anonymous) @ (index):106
c @ jquery-3.4.1.min.js:2
fireWith @ jquery-3.4.1.min.js:2
l @ jquery-3.4.1.min.js:2
(anonymous) @ jquery-3.4.1.min.js:2
load (async)
send @ jquery-3.4.1.min.js:2
ajax @ jquery-3.4.1.min.js:2
(anonymous) @ (index):96