简体   繁体   English

php readfile八位字节流的JavaScript处理

[英]Javascript handling of php readfile octet stream

OK, I've had a good read through the "Related Questions" section and I haven't found this answer. 好的,我已经很好地阅读了“相关问题”部分,但没有找到这个答案。

I'm using an ajax request to force a php download . 我正在使用ajax请求来强制php下载 Things are working fine on the PHP end. 在PHP端一切正常。 Let's say I've got a stream of data called DATA . 假设我有一个名为DATA的数据流。 Now, I want to pop up a "Save as..." dialog. 现在,我想弹出一个“另存为...”对话框。

The browser has received a string of hex values. 浏览器已收到一串十六进制值。 Now, what do I do with this DATA on the client (javascript) side? 现在,如何在客户端(javascript)上使用此DATA

This is the PHP code that I'm using, per the link above: 根据上面的链接,这是我正在使用的PHP代码:

header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename=$file');
readfile($file);

I've tried 我试过了

  • window.open( DATA ) -> hex stream doesn't exist (of course) window.open( DATA )->十六进制流不存在(当然)
  • submitting a form with the action as DATA -> same problem 提交一个动作为DATA的表单->同样的问题

BTW, If I echo the file from PHP then use window.open, it works sometimes. 顺便说一句,如果我从PHP回显文件,然后使用window.open,它有时会起作用。 But not for txt files or jpgs etc. 但不适用于txt文件或jpg等。

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");

...which pops up a box every time. ...每次都会弹出一个框。 The problem with the ajax request was that the returned file stream was interpreted as XMLHttpRequestObj.reponseText . Ajax请求的问题在于返回的文件流被解释为XMLHttpRequestObj.reponseText

The browser apparently interprets this differently and doesn't allow the download. 浏览器显然对此有不同的解释,并且不允许下载。

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

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