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

jquery - JavaScript: How to download a file, then force a page reload? - Stack Overflow

programmeradmin0浏览0评论

As the title mentions, I am trying to download a file which is served with associated mime type via PHP script given by href URL, then reload the same page, but can't quite figure it out, here's what I have so far:

<a id="viewAttachmentLink" href="/path/to/script.php?id=123">View Attachment</a>

<script type='text/javascript'>
    jquery('#viewAttachmentLink').bind('click', function() {
        if (myFunction()) {
            window.location.href = "jquery(this).attr('href')";
            setTimeout(location.reload(), 400);
        } else {
            return false;
        }
    });
</script>

With this code, it will reload the page, but appears to not make the call to the PHP script.

As the title mentions, I am trying to download a file which is served with associated mime type via PHP script given by href URL, then reload the same page, but can't quite figure it out, here's what I have so far:

<a id="viewAttachmentLink" href="/path/to/script.php?id=123">View Attachment</a>

<script type='text/javascript'>
    jquery('#viewAttachmentLink').bind('click', function() {
        if (myFunction()) {
            window.location.href = "jquery(this).attr('href')";
            setTimeout(location.reload(), 400);
        } else {
            return false;
        }
    });
</script>

With this code, it will reload the page, but appears to not make the call to the PHP script.

Share Improve this question edited Sep 21, 2018 at 10:57 informatik01 16.4k11 gold badges78 silver badges108 bronze badges asked Dec 5, 2012 at 23:37 Mike PurcellMike Purcell 20k10 gold badges54 silver badges93 bronze badges 9
  • What do you mean by download a file? Are you wanting to load the resource to execute something in the browser or download the file to the user's puter? Is it a typo the line setting href to a string? – Andrew Hubbs Commented Dec 5, 2012 at 23:42
  • A few oddities: 1. what is myFunction()? 2. it would be window.location.href = $(this).attr('href'); 3. why do you need to reload() when you're already setting the href to something else? – Brian Cray Commented Dec 5, 2012 at 23:44
  • @AndrewHubbs: Download the file, then reload the page. – Mike Purcell Commented Dec 5, 2012 at 23:46
  • @BrianCray: MyFunction is just a placeholder call indicating that I am making a call to another function which will return true/false. Didn't want to add all that code as it doesn't pertain to the question. – Mike Purcell Commented Dec 5, 2012 at 23:47
  • 3 I was able to sort of resolve the situation by adding 'target=_new' to the link definition. Now when link is clicked the download will force open a new window where the remote php script will dictate header content, etc. The browser then downloads the file and closes the window, then the main window reloads. Going to leave this open if anyone has a more elegant solution. – Mike Purcell Commented Dec 6, 2012 at 0:49
 |  Show 4 more ments

2 Answers 2

Reset to default 2

As mentioned in the ments, I was able to get around the issue by adding a target="_new" attribute to the link. So when the link was clicked it would send the request to the remote php script to another window, which would control the headers and start downloading the file, and the original window would reload as needed.

I did a file download and status update (through a header) like this:

<a id="downloadData">Download data...</a>

<script type="text/javascript">
    $('#downloadData').on('click', function () {
        $.ajax({
            url: '/data',
            method: 'GET',
            success: function (data, textStatus, request) {
                var output = request.getResponseHeader('output');
                //refresh page here and use the output
                //console.log(output);
                var a = document.createElement('a');
                var url = '/data';
                a.href = url;
                a.click();
                //save file
            },
            error: function (e) {
                console.log(e);
            }
        });
    });
</script>
发布评论

评论列表(0)

  1. 暂无评论