开发者

Javascript handling of php readfile octet stream

开发者 https://www.devze.com 2023-01-20 12:37 出处:网络
OK, I\'ve had a good read through the \"Related Questions\" section and I haven\'t found this answer.

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. 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.

The browser has received a string of hex values. Now, what do I do with this DATA on the client (javascript) side?

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

I've tried

  • window.open(DATA) -> hex stream doesn't exist (of course)
  • submitting a form with the action as DATA -> same problem

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

...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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号