Forcing “Save As” dialog via jQuery GET

前端 未结 3 1669
遇见更好的自我
遇见更好的自我 2021-01-15 12:31

I\'m calling a jQuery \"GET\" on the test.php file code below.

I\'m trying to get the script to pop a \"Save As\" dialog on the resulting test.ini file to allow it t

相关标签:
3条回答
  • 2021-01-15 13:11

    You can't get an ajax request to show a "Save As" dialog, but what you CAN do is insert a hidden iframe element in the page, then set the source of that iframe to the url you want the user to download. Voila, there's your Save As.

    Here's a copy and paste example:

    $('a#linky').click(function(){
      var iframe = document.createElement("iframe"); 
      iframe.src = 'http://example.com/branding.zip'; 
      iframe.style.display = "none"; 
      document.body.appendChild(iframe);
      return false;
    });
    
    0 讨论(0)
  • 2021-01-15 13:17

    You don't need an AJAX for this. Just navigate to the php in question and in that php use

    header('Content-disposition: attachment;filename=whatever.dat');
    

    This will pop-up the "save as" dialogue box and you'll remain on the original page.

    0 讨论(0)
  • 2021-01-15 13:32

    An AJAX request can't spawn the file download dialog. Consider instead opening your download target in a new window.

    0 讨论(0)
提交回复
热议问题