Tuesday, 15 September 2015

jquery - Having a user download a file through JavaScript? -



jquery - Having a user download a file through JavaScript? -

i trying have button users can click download file, file may not exist because zipped file of other files , has generated. checking ajax 1 time recieve proper url i'm not sure how have user download it.

window.open(link, '_blank'); tries open window download file, browsers prevent , treat pop-up. best practice having user download file this? thanks.

here js function using reference:

function getdownloadedfiles() { var interval_time = 3000, $projectview = $('#project-view'), id = $projectview.data("project-id"); $.ajax({ type: "get", url: ajax_url + id, success: function(data) { if (data.success) { var link = data.profiler.link; window.open(link, '_blank'); } else { settimeout(getdownloadedfiles, interval_time); } } }); }

in end, right solution download file using javascript/jquery , using wrong url.

i setting link data.profiler.link when data.data.link , confused myself.

here final code:

function getdownloadedfiles() { var interval_time = 3000, $projectview = $('#project-view'), id = $projectview.data("project-id"); $.ajax({ type: "get", url: ajax_url + id, success: function(data) { if (data.success) { var link = data.data.link, hiddeniframeid = 'hiddendownloader', iframe = document.getelementbyid(hiddeniframeid); if (iframe === null) { iframe = document.createelement('iframe'); iframe.id = hiddeniframeid; iframe.style.display = 'none'; document.body.appendchild(iframe); } iframe.src = link; } else { settimeout(getdownloadedfiles, interval_time); } } }); }

javascript jquery ajax browser

No comments:

Post a Comment