在下载按钮中调用Ajax,JavaScript,J查询 [英] Call Ajax, JavaScript, J query in download button

查看:63
本文介绍了在下载按钮中调用Ajax,JavaScript,J查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的剧本:

My Script:

<script>
           
            var myTrigger;
            var progressElem = $('#progress');
            $.ajax({
                type: 'GET',
                url: 'download.php',
                
                beforeSend: function(thisXHR)
                {
                    myTrigger = setInterval(function()
                    {
                        if (thisXHR.readyState > 2)
                        {
                            var totalBytes = thisXHR.getResponseHeader('Content-length');
                            var dlBytes = thisXHR.responseText.length;
                            (totalBytes > 0) ? progressElem.html(Math.round((dlBytes / totalBytes) * 100) + "%") : progressElem.html(Math.round(dlBytes / 1024) + "K");
                        }
                    }, 200);
                },
                complete: function()
                {
                    clearInterval(myTrigger);
                },
                success: function(response)
                {
                    // Process XML
                }
            });
                     
        </script>





我的HTML:





My HTML:

<body>
        <form  enctype="multipart/form-data">
            <iframe style="display: none"></iframe>
            <div id="progress" style="width:500px;border:1px solid #ccc;"></div>
            <!-- Progress information -->
            <div id="information" style="width"></div>
            <input type="text" name="file">
            <button id="download" >Download</button>
     <!--<input type="file" name="file" id="file"><br>
     <input type="submit" name="submit" value="Submit">-->
        </form>

    </body>





我的PHP:



My PHP:

<?php

if(empty($_POST['file'])){
	exit;
}

// Sanitizing the filename:
$filename = $_POST['file'];

// Outputting headers:
header("Cache-Control: ");
header("Content-type: application/octet-stream");
header("Content-Description: File Transfer");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($filename));
header('Content-Disposition: attachment; filename="'.$filename.'"');
  readfile($filename);



?>



我不知道如何在我的下载按钮中调用此JavaScript。而且我不知道如何计算我的文件的总大小以及在客户端pc中下载了多少字节。



如果您根据需要制作我的代码,对我有帮助。

推荐答案

' #progress');


.ajax({
type:' GET'
url:' download.php'

beforeSend:< span class =code-keyword> function (thisXHR)
{
myTrigger = setInterval( function ()
{
if (thisXHR.readyState> 2
{
var totalBytes = thisXHR.getResponseHea der(' Content-length');
var dlBytes = thisXHR.responseText.length;
(totalBytes> 0 )? progressElem.html( Math .round((dlBytes / totalBytes)* 100 )+ ):progressElem.html( Math .round( dlBytes / 1024 )+ K );
}
}, 200 );
},
complete: function ()
{
clearInterval(myTrigger);
},
成功:功能(回复)
{
/ / 处理XML
}
});

< / script>
.ajax({ type: 'GET', url: 'download.php', beforeSend: function(thisXHR) { myTrigger = setInterval(function() { if (thisXHR.readyState > 2) { var totalBytes = thisXHR.getResponseHeader('Content-length'); var dlBytes = thisXHR.responseText.length; (totalBytes > 0) ? progressElem.html(Math.round((dlBytes / totalBytes) * 100) + "%") : progressElem.html(Math.round(dlBytes / 1024) + "K"); } }, 200); }, complete: function() { clearInterval(myTrigger); }, success: function(response) { // Process XML } }); </script>





我的HTML:





My HTML:

<body>
        <form  enctype="multipart/form-data">
            <iframe style="display: none"></iframe>
            <div id="progress" style="width:500px;border:1px solid #ccc;"></div>
            <!-- Progress information -->
            <div id="information" style="width"></div>
            <input type="text" name="file">
            <button id="download" >Download</button>
     <!--<input type="file" name="file" id="file"><br>
     <input type="submit" name="submit" value="Submit">-->
        </form>

    </body>





我的PHP:



My PHP:

<?php

if(empty(


_POST [' file'])){
exit ;
}

// 清理文件名:
_POST['file'])){ exit; } // Sanitizing the filename:


这篇关于在下载按钮中调用Ajax,JavaScript,J查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆