title: AJAX: Send file to Servlet and open response in new window
body: <p>in my application, users can edit an ODF file via WebODF (<a href="http://webodf.org/" rel="nofollow">http://webodf.org/</a>). On save, i want to send the edited file to a servlet, have it convert to PDF via ODFDOM (<a href="http://code.google.com/p/xdocreport/wiki/ODFDOMConverterPDFViaIText" rel="nofollow">http://code.google.com/p/xdocreport/wiki/ODFDOMConverterPDFViaIText</a>) and open in a new window.</p>
  
  <p>Currently i am trying to do this via AJAX. Everything works fine up to the point where i try to open the received PDF file.</p>
  
  <p>My Javascript:</p>
  
  <pre><code>function showPDF(pServletUrl)
          {               
              var successCallback = function(pData)
              {
                  var mimetype = "application/vnd.oasis.opendocument.text";
                  var blob = new Blob([pData.buffer], {type: mimetype});
                  var formData = new FormData();
                  formData.append("file", blob, "test.odt");
                  jQuery.ajax({
                      type: "POST", 
                      url: pServletUrl,
                      async: false, 
                      data: formData,
                      processData: false,
                      contentType: false, 
                      success: function(pSuccessData) 
                      { 
                          window.open(pSuccessData);
                      }, 
                      error: function(pErrorData) 
                      { 
                          console.log(pErrorData); 
                      }
                  });
              }
  
              var errorCallback = function(data)
              {
                  console.log(error);
              }
  
              _canvas.odfContainer().createByteArray(successCallback, errorCallback);
          }
  </code></pre>
  
  <p>My servlet:</p>
  
  <pre><code>public void handleRequest(HttpServletRequest pRequest, HttpServletResponse pResponse) throws ServletException, IOException
  {
      BufferedInputStream tBufferedInput = null;
      BufferedOutputStream tBufferedOutput = null;
  
      try 
      {
          List&lt;FileItem&gt; tItems = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(pRequest);
          for (FileItem tItem : tItems) 
          {
              if (!tItem.isFormField()) 
              {
                  String tFieldname = tItem.getFieldName();
                  String tFilename = FilenameUtils.getName(tItem.getName());
                  InputStream tFilecontent = tItem.getInputStream();
  
                  if("file".equals(tFieldname))
                  {
                      tBufferedInput = new BufferedInputStream(tFilecontent);
                      pResponse.reset();
                      pResponse.setHeader("Content-Type", "application/pdf");
                      pResponse.setHeader("Content-Disposition", "inline; filename=\"" + "test.pdf" + "\"");
                      tBufferedOutput = new BufferedOutputStream(pResponse.getOutputStream(), 10240);
  
                      this.getOdtAsPdf(tBufferedInput, tBufferedOutput);
  
                      tBufferedOutput.flush();
                  }
              }
          }
      }   
      catch(Exception e)
      {
          e.printStackTrace();
      }
      finally
      {
          try
          {
              tBufferedInput.close();
              tBufferedOutput.close();
          }
          catch(Exception e)
          {
  
          }
      }
  }
  
  private void getOdtAsPdf(InputStream pInputStream, OutputStream pOutputStream) throws Exception
  {
      OdfDocument tOdfDocument = OdfDocument.loadDocument(pInputStream);
      PdfOptions tPdfOptions = PdfOptions.create();
      PdfConverter.getInstance().convert(tOdfDocument, pOutputStream, tPdfOptions);
  }
  </code></pre>
  
  <p>It seems like Javascript wants to parse the recieved PDF file as a URL and (obviously) fails doing so. Is there a way to just open the file in a new window or do i have to find another way to do this?</p>