【问题标题】:Requesting to a servlet file downloading via ajax(no jquery please..)请求通过 ajax 下载 servlet 文件(请不要使用 jquery ..)
【发布时间】:2015-06-15 21:38:27
【问题描述】:

流程是这样的:

  1. 从 web(jsp) 我上传了一些 pdf 文件(通过 ajax 提交)
  2. 我在后端合并了这些 pdf
  3. 我通过 ajax 得到响应(合并的 pdf) --> 开始文件下载...

我在第三步时遇到问题。

我只包含了我提交文件以上传(发布请求)并开始下载的相关代码。 我还放了一个直接链接,它在 get 方法中调用相同的步骤并且可以工作。

我的问题在哪里? 提前谢谢...

这里是jsp正文标签

<a href="/TestAjaxServletDownload/DownloadServlet" >
    download
</a>

<p><input id="sampleFile5" name="sampleFile5" type="file" /></p>

<p><input id="uploadBtn" type="button" value="Upload" onClick="javascript:performAjaxSubmit();"></input></p>

这是我的 javascript 标签内容

function performAjaxSubmit() {

        var sampleFile1 = document.getElementById("sampleFile5").files[0];
        var formdata = new FormData();

        formdata.append("sampleFile", sampleFile1);

        var xhr = new XMLHttpRequest();       

        xhr.onload = function() {
            if(xhr.readyState == 4 && xhr.status == 200) {
//              alert("ok..." + xhr.responseText);
                //?????????????????????????????
                document.location=xhr.responseText;               
            }
        };   

        xhr.open("POST","/TestAjaxServletDownload/DownloadServlet", true);
        xhr.send(formdata);

    }

这是我的 web.xml serverlet 映射标签

<servlet>
    <description></description>
    <display-name>DownloadServlet</display-name>
    <servlet-name>DownloadServlet</servlet-name>
    <servlet-class>test.DownloadServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DownloadServlet</servlet-name>
    <url-pattern>/DownloadServlet</url-pattern>
  </servlet-mapping>

这是我的 servlet 代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("DO GET SERVLET MERGE");
        execute (request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        System.out.println("DO POST SERVLET MERGE");
        execute (request, response);
    }


    protected void execute(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        File downloadFile = new File("c:\\data\\example.pdf");
        System.out.println("++++" + downloadFile.getAbsolutePath());
//      System.out.println(uploadPathTemp+mergeFileName);
        FileInputStream inStream = new FileInputStream(downloadFile);
         // obtains ServletContext
         ServletContext context = getServletContext();

         // gets MIME type of the file
         String mimeType = context.getMimeType(downloadFile.getCanonicalPath());
         if (mimeType == null) {        
             // set to binary type if MIME mapping not found
             mimeType = "application/octet-stream";
         }

         // modifies response
         response.setContentType(mimeType);
         response.setContentLength((int) downloadFile.length());

         // forces download
         String headerKey = "Content-Disposition";
         String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
         System.out.println(downloadFile.getName());
         response.setHeader(headerKey, headerValue);

         // obtains response's output stream
         OutputStream outStream = response.getOutputStream();

         byte[] buffer = new byte[4096];
         int bytesRead = -1;

         while ((bytesRead = inStream.read(buffer)) != -1) {
             outStream.write(buffer, 0, bytesRead);
         }

         inStream.close();
         outStream.close();

    }

【问题讨论】:

  • 您的实际问题是什么?你有错误吗?
  • 嗨,埃文,谢谢你的问题....实际上,如果我单击 href,我会通过 doGet 方法调用执行方法(在 servlet 中),一切都完成了。但是如果我点击上传按钮,通过 doPost 调用相同的执行方法,下载不会开始。我需要 doPost 方法有效....谢谢
  • 您的意思是说文件下载正常但上传不正常?
  • 不,我需要调用 ajax 方法的下载工作.....

标签: javascript ajax jsp servlets


【解决方案1】:

怎么改

<a href="/TestAjaxServletDownload/DownloadServlet" > download </a>

<a id="pdfLink" href="/TestAjaxServletDownload/DownloadServlet" > download </a>

然后使用document.getElementById('pdfLink').click()?

【讨论】:

  • No Boom,该链接仅用于测试......我需要通过 ajax 调用 servlet 的上传按钮......
  • 您仍然可以使用该链接进行测试,一旦有效,请将其更改为不可见。 click() 仍然可以工作。这是一种方式。
猜你喜欢
  • 2015-09-04
  • 2012-11-11
  • 1970-01-01
  • 2011-10-16
  • 2014-01-16
相关资源
最近更新 更多