开发者

downloading thru ajax and iframe?

开发者 https://www.devze.com 2023-03-04 20:49 出处:网络
I have this small piece if code: $.post(\'exportToExcel.php\', function(data) { var iframe = document.createElement(\"iframe\");

I have this small piece if code:

$.post('exportToExcel.php', function(data) {

    var iframe = document.createElement("iframe");
    iframe.src = ""+data;
    iframe.style.display = "none"开发者_高级运维;

    document.body.appendChild(iframe);
});

But nothing happens, I'm trying to download a file seamless from the server thru jquery's ajax.

Anyone got a good fix?


If you are simply trying to direct the user to a file, download you should use a regular link or use location.href = 'my_url'; to send the browser there directly.

Alternatively, if you really want to do it this 'seamless' way, don't use an AJAX POST. Just link directly to exportToExcel.php in the iframe.

If exportToExcel.php requires the use of POST, make the iframe visible and when exportToExcel.php is called via GET, provide a 'Download' button which is the same size as the iframe and, when clicked, submits a regular HTML form to the same URL via POST.

0

精彩评论

暂无评论...
验证码 换一张
取 消