XMLHtt prequest在浏览器中打开PDF [英] XMLHttpRequest to open PDF in browser

查看:130
本文介绍了XMLHtt prequest在浏览器中打开PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的XMLHtt prequest,然后由POST方法发送的文件名,打开浏览器中的PDF文件。

  xmlhttp.open(POST,pdf.php,真正的); //更改
   xmlhttp.setRequestHeader(内容型,应用程序/ x-WWW的形式urlen codeD);
   xmlhttp.send(文件=+输入);
 

这是可能的或XMLHtt prequest仅仅是对HTML?

解决方案
  1. 这是不可能通过XMLHtt prequest做,如果你实际上是查询网址返回的PDF数据。

    为什么呢?由于响应是包含原料的PDF数据的HTTP响应。还有就是要与包含在数据PDF文件的渲染替换当前文档的DOM内容概不JavaScript的能力,即使你通过的 responseText的`属性(参见<一href="http://www.w3.org/TR/XMLHtt$p$pquest/#the-responsetext-attribute">http://www.w3.org/TR/XMLHtt$p$pquest/#the-responsetext-attribute).

  2. 你可以做的是生成一个PDF文件转换成通过URL访问临时文件从您的Web服务器,然后让脚本发回的网址访问该文件。

    当你的反应处理程序处理的URL,它可以:

    • 通过改变重新加载当前页面 window.location.href = new_pdf_url

    • 它加载到一个&LT; IFRAME&GT; 通过更改iframe的的src 属性<当前文档中/ P>

    • 在一个单独的窗口中的window.open(new_pdf_url,XXX)

      打开它

      请注意,您仍然需要一个URL到一个临时文件的位置,打开一个新的窗口

I want to do XMLHttpRequest and then open a PDF in the Browser by sending the filename by POST method.

   xmlhttp.open("POST","pdf.php",true); //CHANGE
   xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
   xmlhttp.send("file="+input);

Is that possible or XMLHttpRequest is just for HTML?

解决方案

  1. It is not possible to do via XMLHttpRequest if the URL you are querying actually returns the PDF data.

    Why? Because the response is an HTTP response which contains raw PDF data. There is no JavaScript ability to replace the current document's DOM contents with a rendering of a PDF contained in that data, even though you DO have access to the data via responseText` attribute (also see http://www.w3.org/TR/XMLHttpRequest/#the-responsetext-attribute).

  2. What you CAN do is to generate a PDF file into a temporary file accessible via a URL from your web server, and then have the script send back the URL for accessing that file.

    When your response handler processes the URL, it can either:

    • Re-load the current page by changing window.location.href = new_pdf_url

    • Load it in an <iframe> inside the current document by changing iframe's src attribute

    • Open it in a separate window by window.open(new_pdf_url, XXX)

      Please note that you STILL need a URL to a temp file location to open a new window

这篇关于XMLHtt prequest在浏览器中打开PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆