【问题标题】:AJAX: Send file to Servlet and open response in new windowAJAX:将文件发送到 Servlet 并在新窗口中打开响应
【发布时间】:2014-04-28 07:37:31
【问题描述】:

在我的应用程序中,用户可以通过 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);
        }

我的小服务程序:

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,但(显然)这样做失败了。有没有办法在新窗口中打开文件或者我必须找到另一种方法来做到这一点?

【问题讨论】:

    标签: java javascript ajax xdocreport webodf


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您可以在进行 ajax 调用后使用嵌入标签来显示您的 blob。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-01
        • 2013-03-07
        • 1970-01-01
        • 2017-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多