使用jQuery从服务器下载二进制文件

Download binary file from server using jQuery

本文关键字:下载 二进制文件 服务器 jQuery 使用      更新时间:2023-11-28

不知怎么的,我的旧帖子被删除了,不管怎样,有人能帮我吗我正在尝试使用javascript将二进制文件从服务器下载到客户端。下面的代码运行良好,但它使用的是mootools框架,但我的要求是使用jQuery。我对编程很陌生,所以如果有人帮助就足够了

<head>
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools-yui-compressed.js"> </script>
   <script type="text/javascript">
       window.addEvent("domready",function(){
           //Creating a new AJAX request that will request 'input.txt' 
           //from the current directory
           var csvRequest = new Request({
                  url:"input.txt",
                  onSuccess:function(response){
                  $("textResponse").value = response;
               }
           }).send(); //Don't forget to send our request!
        });
        </script>
    </head>

您可以尝试$.ajax对象来完成同样的事情。要下载相同的文本文件,请使用以下代码:

$.ajax({
    url: 'file.txt'
})
.done(function(data){
    $("#textResponse").value=data;//suppose textResponse is ID of input
})
.fail(function( jqXHR, textStatus, errorThrown){
    //handle failure of request
});

有关$.ajax的更多信息,请点击此处。

只是好奇:使用Mootools有什么问题?

无论如何,试试这个:

<head>
   <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
   <script type="text/javascript">
    $(function(){
        $.ajax({
            url: 'file.txt'
        }).done(function (data) {
            $("#textResponse").val(data);
        })
    });
    </script>
</head>