问题描述
好的,我已经仔细阅读了相关问题"部分,但没有找到这个答案.
OK, I've had a good read through the "Related Questions" section and I haven't found this answer.
我正在使用ajax请求来强制进行php下载.在PHP端一切正常.假设我有一个名为 DATA
的数据流.现在,我想弹出一个另存为..."对话框.
I'm using an ajax request to force a php download. Things are working fine on the PHP end. Let's say I've got a stream of data called DATA
. Now, I want to pop up a "Save as..." dialog.
浏览器已收到一串十六进制值.现在,如何在客户端(javascript)端使用此 DATA
?
The browser has received a string of hex values. Now, what do I do with this DATA
on the client (javascript) side?
根据上面的链接,这是我正在使用的PHP代码:
This is the PHP code that I'm using, per the link above:
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename=$file');
readfile($file);
我尝试过
- window.open(
DATA
)->(当然)不存在十六进制流 - 提交一个动作为
DATA
->相同问题的表单
- window.open(
DATA
) -> hex stream doesn't exist (of course) - submitting a form with the action as
DATA
-> same problem
顺便说一句,如果我从PHP中回显文件,然后使用window.open,它有时会起作用.但不适用于txt文件或jpg等.
BTW, If I echo the file from PHP then use window.open, it works sometimes. But not for txt files or jpgs etc.
我在其他网站上看到了此功能-他们是如何做到的?预先感谢.
I've seen this working on other sites - how are they doing it? Thanks in advance.
推荐答案
以下是我正在寻找的答案:
Here's the answer I was looking for:
window.open("downloadPage.php");
...每次都会弹出一个框.Ajax请求的问题在于返回的文件流被解释为 XMLHttpRequestObj.reponseText
.
...which pops up a box every time. The problem with the ajax request was that the returned file stream was interpreted as XMLHttpRequestObj.reponseText
.
浏览器显然对此有不同的解释,并且不允许下载.
The browser apparently interprets this differently and doesn't allow the download.
这篇关于php readfile八位字节流的Javascript处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!