[英]Javascript: getting Audio via POST
I may be coming at this from completely the wrong angle (if this is the case then please feel free to tell me so) but.... 我可能会从完全错误的角度来看这个(如果是这种情况那么请随时告诉我)但......
I have a music app in which a user can upload a music track and then later on preview it. 我有一个音乐应用程序,用户可以在其中上传音乐曲目,然后再预览它。
The directory where the audio is stored is protected with .htaccess to prevent unauthorised access: 存储音频的目录受.htaccess保护,以防止未经授权的访问:
deny from all
Protecting this directory obviously prevents the files being accessed by Javascript: 保护此目录显然会阻止Javascript访问文件:
var track = new Audio('path/to/file');
I want to be able to validate the user and then return the track through another script by passing extra info with the request for the page (too much data for a GET request). 我希望能够验证用户,然后通过传递页面请求的额外信息(GET请求的数据太多),通过另一个脚本返回轨道。
var track = new Audio('path/to/script/to/return/audio');
Is this possible? 这可能吗? Pretty sure it's not so if you can think of a solution, I'd be keen to hear. 如果你能想到一个解决方案,我很想知道它并不是这样。
Why don't you serve your audio file via a php script that would 为什么不通过php脚本提供音频文件
Something like (inspired by php output file on disk to browser SO question) 喜欢的东西(灵感来自磁盘上的php输出文件到浏览器 SO问题)
in get-audio.php
file 在get-audio.php
文件中
<?php
// check user credentials
if($_GET['auth_token'] != '12345') exit;
// read the audio file
$name = $_GET['path'];
if (file_exists($name)) {
// send the right headers
header("Content-Type: audio/mp3");
header("Content-Length: " . filesize($name));
// dump the file and stop the script
readfile($name)
exit
}
And then, in javascript : 然后,在javascript中:
var track = new Audio('get-audio.php?auth_token=12345&path=path/to/file');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.