简体   繁体   中英

Simulate a href click - Download File

I have a "a href" modified from Jquery, I need to run that "href" to download the file, but the event () click does not work.

 // window.location = 'data:'+part.mimeType+';base64,'+resp.data.replace(/-/g, '+').replace(/_/g, '/'); ---> i cant modify file name
              descarga = $("#links");


               var link = 'data:' + part.mimeType + ';base64,' + resp.data.replace(/-/g, '+').replace(/_/g, '/');
              // inline.append('<a href="' + link + '" style="display: block" download="'+part.filename+'">' + part.filename + '</a>');

              descarga.attr("href", link);
              descarga.text('text here');
              descarga.attr("download", part.filename);
              document.location.href = link;
              descarga.click(); 

I can not do "windows.location" because it is unpredictable that the file has a certain name and as found in the internet do it in "windows.location" is impossible

i find the solution: =)

   var links = document.createElement('a');
                  document.body.appendChild(links); 
                  links.download = part.filename;
                  links.href = link;
                  links.click();
                  document.body.removeChild(link); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM