简体   繁体   English

jQuery / Ajax显示“另存为”对话框,用于下载从Servlet发送的文件

[英]jQuery/Ajax to show Save As dialog for downloading file sent from servlet

I have read many questions/anwsers in the site regarding this issue and not sure any one of them can solve my problem. 我已经在网站上阅读了许多有关此问题的问题/答案,但不确定其中任何一个都可以解决我的问题。 Below is how my application supposed to work: 以下是我的应用程序应如何工作:

  1. The page will show a list of files that available on the server that have been downloaded before. 该页面将显示以前已下载的服务器上可用文件的列表。
  2. The page will also have a button when clicked will send a request to the servlet that will collect data on the server and save it as the zip file on a predefined directory. 单击该页面时还将有一个按钮,它将向Servlet发送一个请求,该Servlet将收集服务器上的数据并将其另存为zip文件到预定义目录中。 The servlet than reads the files and send it back to the client as a binary stream with the following response content type and header information: content-type = application/octet-stream header=content-Disposition, attachment; 然后,servlet读取文件并将其作为具有以下响应内容类型和标头信息的二进制流发送回客户端:content-type = application / octet-stream header = content-Disposition,Attach; filename=... header=content-type, application/x-download filename = ... header =内容类型,应用程序/ x-下载
  3. The requirement is that the request is asynchronous since the servlet may take a long time to collect the huge a mount of data which may exceed 100 MB. 要求是异步的,因为servlet可能需要很长时间才能收集可能超过100 MB的大量数据。
  4. I have to disable the button while the request is in progress and know when the request is completed so that I can send refresh to list to show the new file. 我必须在请求进行时禁用该按钮,并且知道请求何时完成,以便可以将刷新发送到列表以显示新文件。

I don't think the solutions using iframe, Window.location.href, or using html form will satisfy my requirements. 我认为使用iframe,Window.location.href或html表单的解决方案不能满足我的要求。 Please let me know if there is any solution for my problem. 请让我知道我的问题是否有解决方案。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

-Tam -谭

As you writed it in your question title, i suggeste that you used a bit of javascript, either JQuery or prototype can do the job. 当您在问题标题中编写它时,我建议您使用一些javascript,无论是JQuery还是原型都可以完成这项工作。

Since I know more about this last one, i will give some piece of code 既然我对这最后一个了解更多,我将给出一些代码

here is the process I understand for your case. 这是我为您了解的过程。

First your server send a page with a form. 首先,您的服务器发送带有表单的页面。 Then the user select a file and click the send button, the page does not reload and use Ajax to do the job then JQuery get the data and send it to your server via Ajax ( request(); ) then The server send back the results, and you can display it displayFilesList(); 然后用户选择一个文件并单击send按钮,该页面不会重新加载并使用Ajax来完成工作,然后JQuery获取数据并将其通过Ajax( request(); )发送到您的服务器,然后服务器将结果发送回,您可以将其显示为displayFilesList();

your_form_id is your form id your_form_id是您的表单ID

submitForm is the name of the function called when your form will be submitted submitForm是表单提交时调用的函数的名称

displayFilesList will be the function called when your server will send back the results this function also expect you to send back the results in a json format 当您的服务器发送回结果时, displayFilesList将被调用此函数,该函数也希望您以json格式发送回结果

$('#your_form_id').submit(submitForm);

function submitForm()
{
    var data = $('#your_form_id').serializeArray();
    //Code to remove the send button here
    //Code to insert a loading.gif
    request("/your/url", data, displayFilesList);
    return false;
}

function    displayFilesList(jsonResult)
{
    var data = jQuery.parseJSON(jsonResult);
    //Code to display the files list here
    //
}

function    request(url, paramPost, successCallback, errorCallback)
{
    if ((typeof successCallback == 'undefined') || (successCallback == null))
        successCallback = dispSuccess;
    if ((typeof errorCallback == 'undefined') || (errorCallback == null))
        errorCallback = dispError;

    var AjaxUrl = url;

    $.ajax({    
            type: "POST",
            url: AjaxUrl,
            data: paramPost
           }
          ).done( function(msg)
                  {
                       successCallback(msg);
                  }
          ).fail( function(jqXHR, textStatus, errorThrown)
                  {
                       errorCallback(jqXHR, textStatus, errorThrown);
                  }
          );
}

function    dispError(jqXHR, textStatus, errorThrown)
{
    alert(jqXHR.responseText);
}
function    dispSuccess(html)
{
    alert(html);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM