Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
强制从所有浏览器上的url下载mp3。Javascript/jQuery/reactJS_Javascript_Jquery_Reactjs - Fatal编程技术网

强制从所有浏览器上的url下载mp3。Javascript/jQuery/reactJS

强制从所有浏览器上的url下载mp3。Javascript/jQuery/reactJS,javascript,jquery,reactjs,Javascript,Jquery,Reactjs,我正在为一个项目工作,我必须实现一个功能:mp3下载。我只在AmazonS3上获得了托管文件的url,当我点击一个按钮时,它应该会触发下载。 我尝试了HTML5的下载属性,但它在mozilla上不起作用,对我来说也是不可能的 <a href="www.myurl.cloudfront.net/mymp3.mp3" download> Download </a> 你们有什么解决方案可以让它在每个浏览器上都工作吗?或任何其他解决方案 谢谢 NaeljQuery和Reac

我正在为一个项目工作,我必须实现一个功能:mp3下载。我只在AmazonS3上获得了托管文件的url,当我点击一个按钮时,它应该会触发下载。 我尝试了HTML5的下载属性,但它在mozilla上不起作用,对我来说也是不可能的

<a href="www.myurl.cloudfront.net/mymp3.mp3" download> Download </a>

你们有什么解决方案可以让它在每个浏览器上都工作吗?或任何其他解决方案

谢谢


Nael

jQuery和React不能很好地协同工作,至少不能操作DOM节点

如果使用jQuery进行AJAX调用并作为视图层进行响应,则可以将onClick处理程序连接到标记上,这样当用户单击时,您就可以在任何需要的地方获得AJAX请求:

class YourComponent extends Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this)
  }
  handleClick(evt) {
    const { currentTarget } = evt;
    // currentTarget holds information of where you clicked, you can get your url from here
    $.ajax({
      //Your ajax call here
    }).then(data => //Handle your data, setState, etc.)
  }
  render() {
    <div>
      //Everything else
      <a onClick={this.handleClick} url='...'> Download</a>
    </div>
  }
};
class YourComponent扩展组件{
建造师(道具){
超级(道具);
this.handleClick=this.handleClick.bind(this)
}
handleClick(evt){
const{currentTarget}=evt;
//currentTarget保存您单击的位置的信息,您可以从这里获取url
$.ajax({
//你的ajax呼叫在这里
}).然后(数据=>//处理数据、设置状态等)
}
render(){
//其他一切
下载
}
};

好的,但是即使在AJAX中,我如何下载托管的mp3呢?我相信你可以使用
window.open(url)
,这应该会触发下载。不,mp3文件的问题会出现在window.open()或其他类似href的东西上。。只需打开html5播放器即可查看此链接