File upload does not work on firefox

I wrote this piece of code that works fine on Google Chrome and Opera, but doesn't work on 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); } 

What's wrong? please guide me.

+6
source share
1 answer

This should work (I figured this out by looking at the FileSaver.js code):

 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", true, false, window, 0, 0, 0, 0, 0 , false, false, false, false, 0, null ); save.dispatchEvent(event); } 

(The main problem is that you need to use an event like MouseEvent for firefox, not an event. This code will also work in Chrome).

+16
source

Source: https://habr.com/ru/post/959089/


All Articles