我已经编写了这段代码,这些代码在Google Chrome和Opera上运行得很完美,但不适用于Firefox
function onSaveJPG(url,n){ var save = document.createElement('a'); save.href = url; save.target = '_blank'; save.download = 'Image no '+n+'.jpeg' || url; var event = document.createEvent('Event'); event.initEvent('click',true,true); save.dispatchEvent(event); (window.URL || window.webkitURL).revokeObjectURL(save.href); }
怎么了?请指导我.
解决方法
这应该工作(我通过查看FileSaver.js代码找出它):
function onSaveJPG(url,n){ var save = document.createElement('a'); save.href = url; save.download = 'Image no '+n+'.jpeg' || url; var event = document.createEvent("MouseEvents"); event.initMouseEvent( "click",false,window,null ); save.dispatchEvent(event); }
(主要问题是你需要为firefox使用MouseEvent类型事件而不是事件.此代码也适用于Chrome).