请求通过ajax下载servlet文件(请不要使用jquery ..) [英] Requesting to a servlet file downloading via ajax(no jquery please..)

查看:123
本文介绍了请求通过ajax下载servlet文件(请不要使用jquery ..)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过程是这样的:

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

我在执行第三步时遇到问题.

I'm having issues with the third step.

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

I've included only the relevant code where I submit the file to upload (post request) and start the download. I put also a direct link, that calls the same steps in get method and works.

我的问题在哪里? 预先感谢...

Where is my problem? Thanks in advance...

这是jsp body标记

Here is the jsp body tag

<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标签内容

Here is my javascript tag content

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

Here is my web.xml servelet mapping tags

<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代码

Here is my servlet code

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

    }

推荐答案

关于更改

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

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

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

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

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

这篇关于请求通过ajax下载servlet文件(请不要使用jquery ..)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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