简体   繁体   English

是否可以更改ajax结果的内容/ MIME类型?

[英]Is it possible to change the content/mime type of ajax results?

I am using output buffering to grab a pdf file from a network share via PHP using a proxy-handler and readfile(). 我正在使用输出缓冲通过PHP使用代理处理程序和readfile()从网络共享中获取pdf文件。 I display this in a new browser tab by specifying the content/mime type. 我通过指定内容/ MIME类型在新的浏览器选项卡中显示此内容。 (http://stackoverflow.com/questions/10121903/php-scandir-explorer-view-to-display-network-share-files-file-via-wamp-wor) (http://stackoverflow.com/questions/10121903/php-scandir-explorer-view-to-display-network-share-files-file-via-wamp-wor)

I use jQuery / Ajax throughout the rest of the application. 我在整个应用程序的其余部分都使用jQuery / Ajax。 Is it possible to inject the raw PDF content into a div tag using ajax but get it to display correctly? 是否可以使用ajax将原始PDF内容注入到div标签中,但可以使其正确显示? Currently the required header (header("Content-type: application/pdf")) seems to be stripped by the ajax request and it is displaying jibberish. 当前,所需的标头(header(“ Content-type:application / pdf”))似乎已被ajax请求剥离,并显示乱码。

Edit: Thankyou all for the comments and answers. 编辑:谢谢大家的评论和回答。 I will respond in more detail and review my options shortly. 我将更详细地回应,并在不久后审查我的选择。

@ Murtaza - the question link above shows how I got to the raw PDF, in addition I have used $.ajax to send the request to the div tag and used an iframe which sort of worked when I rendered it into the code as src=, rather than Ajax. @ Murtaza-上面的问题链接显示了如何获取原始PDF,此外,我还使用$ .ajax将请求发送至div标签,并使用了iframe,当我将其呈现为src = ,而不是Ajax。

@ Brad Christie / Quentin - Perhaps I could use an iFrame or Modal to display the content? @ Brad Christie / Quentin-也许我可以使用iFrame或Modal来显示内容? I have tested pdfobject.js which is good but requires a linked file (eg /files/file.pdf). 我已经测试了pdfobject.js,这很好,但是需要一个链接文件(例如/files/file.pdf)。 It sounds horrific but what if I use PHP to write a temporary file to the web root and then display? 这听起来很可怕,但是如果我使用PHP将临时文件写入Web根目录然后显示该怎么办? The only other thing I can think could be useful might be the jQuery PDF library if anyone has used it? 我认为唯一有用的其他事情可能是jQuery PDF库(如果有人使用过的话)?

@ Ignacio - I did try this with the following code. @ Ignacio-我确实使用以下代码尝试了此操作。 It rendered the jibberish. 它呈现出低调。 I'm probably doing it wrong as I have no experience with iFrames. 我没有iFrame经验,因此可能做错了。 // Where $filename is the raw PDF data "> //其中$ filename是原始PDF数据“>

James. 詹姆士。

You can't "inject the raw PDF content into a div tag"; 您不能“将原始PDF内容注入div标签”; the PDF file is not HTML and must go through a PDF reader plugin. PDF文件不是HTML,必须通过PDF阅读器插件。 Try making it the target of a iframe instead. 尝试使其成为iframe的目标。

Is it possible to inject the raw PDF content into a div tag using ajax but get it to display correctly? 是否可以使用ajax将原始PDF内容注入到div标签中,但可以使其正确显示?

Not really. 并不是的。 You might be able to make something of pdf.js though. 不过,您也许可以制作pdf.js。

Currently the required header (header("Content-type: application/pdf")) seems to be stripped by the ajax request 当前,所需的标头(header(“ Content-type:application / pdf”))似乎已被ajax请求剥离

Whatever means you are using to insert the data is explicitly treating it as text or HTML, not as a PDF. 无论您使用哪种方式插入数据,都将其明确地视为文本或HTML,而不是PDF。 It is your your code is ignoring the content-type. 是您的代码忽略了内容类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM