请求通过ajax下载servlet文件(没有jquery请...)

时间:2015-04-10 08:34:08

标签: javascript ajax jsp servlets

过程是这样的:

  1. 来自web(jsp)我上传了一些pdf文件(通过ajax提交)
  2. 在后端我合并这些pdf
  3. 我通过ajax获得了回复(合并的pdf) - >启动文件下载...
  4. 我遇到了第三步的问题。

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

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

    这是jsp body标签

    <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 servelet映射标记

    <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();
    
        }
    

1 个答案:

答案 0 :(得分:1)

如何改变

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

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

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