首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AJAX:将文件发送到Servlet并在新窗口中打开响应

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

Stack Overflow用户
提问于 2014-04-28 15:37:31
回答 2查看 2.4K关注 0票数 0

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

目前,我正在尝试通过AJAX来实现这一点。在我尝试打开收到的PDF文件之前,一切都很正常。

我的Javascript:

代码语言: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:

代码语言:javascript
复制
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,但(显然)这样做失败了。有没有办法直接在新窗口中打开文件,或者我必须找到其他方法才能做到这一点?

EN

回答 2

Stack Overflow用户

发布于 2014-04-28 16:32:31

不能使用Ajax打开该文件。这是对javascript的安全限制。您有几个变通方法:

  1. 使用了一个插件,它提供了Ajax类型的体验,但是在一个新的window.more details here
  2. have中打开一个文件,一个提交到新窗口的表单。
  3. 另一个选项(不是很好)是将文件存储在<form target=_blank />中,并在AJAX的响应中传递id。然后使用Javascript使用window.open('downloadurl?id')进行调用,这将发送您的

文件的响应。

票数 0
EN

Stack Overflow用户

发布于 2015-12-14 04:10:28

在进行ajax调用之后,您可以使用embed标记来显示blob。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23335169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档