最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - javascript - opening hidden iframe for file download on document ready - Stack Overflow

programmeradmin1浏览0评论

I’m trying to use this trick to open a file download dialog on document ready. The same trick has worked another time for me, but that time the iframe was added after an ajax call. This is the snippet:

<script type="text/javascript">
  $(document).ready(function() {
     var url='/my_file_url/';
     var _iframe_dl = $('<iframe />')
                       .attr('src', url)
                       .hide()
                       .appendTo('body');
  });
</script>

While the iframe is correctly printed in html code, it doesn’t have the expected behaviour: no download file popup appears after loading the page. Any help on why?

I’m trying to use this trick to open a file download dialog on document ready. The same trick has worked another time for me, but that time the iframe was added after an ajax call. This is the snippet:

<script type="text/javascript">
  $(document).ready(function() {
     var url='/my_file_url/';
     var _iframe_dl = $('<iframe />')
                       .attr('src', url)
                       .hide()
                       .appendTo('body');
  });
</script>

While the iframe is correctly printed in html code, it doesn’t have the expected behaviour: no download file popup appears after loading the page. Any help on why?

Share Improve this question edited Apr 22, 2013 at 10:44 franzlorenzon 5,9436 gold badges37 silver badges58 bronze badges asked Apr 22, 2013 at 10:28 LukeLuke 1,84410 gold badges44 silver badges73 bronze badges 7
  • are you sure your syntax is correct ? – Adil Shaikh Commented Apr 22, 2013 at 10:29
  • there is no closing }); – Toni Toni Chopper Commented Apr 22, 2013 at 10:30
  • Are the headers correct for your file url? (if you open it with your browser, do you have the download popup? – krampstudio Commented Apr 22, 2013 at 10:30
  • You aren't closing your $(document).ready(function(){ – silentw Commented Apr 22, 2013 at 10:30
  • 1 Works if you close your function and invocation with }); jsfiddle/aMcNV – Paul S. Commented Apr 22, 2013 at 10:32
 |  Show 2 more ments

1 Answer 1

Reset to default 4

It works just fine, assuming that the MIME is of a type that will start a download, for example application/octet-stream. You may be encountering an issue where the browser is rendering it and not offering a download due to an in-built pdf reader.

$(document).ready(function(){
var url='data:application/octet-stream,hello%20world';
var _iframe_dl = $('<iframe />')
       .attr('src', url)
       .hide()
       .appendTo('body');
});

An alternate solution, if the client is on a modern browser, is to use an <a> with href and download set, then simulate a click on it.

var a = document.createElement('a'),
    ev = document.createEvent("MouseEvents");
a.href = url;
a.download = url.slice(url.lastIndexOf('/')+1);
ev.initMouseEvent("click", true, false, self, 0, 0, 0, 0, 0,
                  false, false, false, false, 0, null);
a.dispatchEvent(ev);
发布评论

评论列表(0)

  1. 暂无评论