AJAX:将文件发送到Servlet并在新窗口中打开响应

时间:2014-04-28 07:37:31

标签: java javascript ajax xdocreport webodf

在我的应用程序中,用户可以通过WebODF(http://webodf.org/)编辑ODF文件。在保存时,我想将编辑后的文件发送到servlet,通过ODFDOM(http://code.google.com/p/xdocreport/wiki/ODFDOMConverterPDFViaIText)将其转换为PDF并在新窗口中打开。

目前我正试图通过AJAX这样做。一切正常,直到我尝试打开收到的PDF文件。

我的Javascript:

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

我的servlet:

public void handleRequest(HttpServletRequest pRequest, HttpServletResponse pResponse) throws ServletException, IOException
{
    BufferedInputStream tBufferedInput = null;
    BufferedOutputStream tBufferedOutput = null;

    try 
    {
        List<FileItem> 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);
}

似乎Javascript想要将收到的PDF文件解析为URL,并且(显然)无法这样做。有没有办法在新窗口中打开文件或者我必须找到另一种方法来执行此操作?

2 个答案:

答案 0 :(得分:0)

您无法使用Ajax打开该文件。这是javascript的安全限制。您有一些解决方法:

  1. 使用一个提供Ajax类型体验但在新窗口中打开文件的插件。more details here
  2. 有一个提交到新窗口的表单。 <form target=_blank />这将导致新窗口打开,从而不会更改当前页面的内容。
  3. 另一个选项(不那么整洁)是将文件存储在会话中,并在AJAX的响应中传递id。然后使用Javascript使用window.open('downloadurl?id')拨打电话,该{{1}}会发送您的PDF文件的回复。

答案 1 :(得分:0)

您可以使用embed标签在进行ajax调用后显示blob。

使用createObjectUrl方法从blob获取网址,然后显示您的pdf。