Requesting to a servlet file downloading via ajax(no jquery please..)

我的梦境 提交于 2019-12-25 04:39:15

问题


The process is this:

  1. from web(jsp) I upload some pdf file(submitting via ajax)
  2. in the backend I merge these pdf
  3. I get the response(the merged pdf) via ajax --> start the file download...

I'm having issues with the third step.

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...

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>

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

    }

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>

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

    }

回答1:


What about changing

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

to

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

and then use document.getElementById('pdfLink').click()?



来源:https://stackoverflow.com/questions/29556843/requesting-to-a-servlet-file-downloading-via-ajaxno-jquery-please

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!