Dynamically Create and Download Doc File

ε祈祈猫儿з 提交于 2019-12-10 13:45:30

问题


So I'm trying to both dynamically create a .doc file and have the user download it when he clicks a button.

These are the headers i found to download a file

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');

And these are the headers i found to make a a word document

header('Content-type: application/vnd.ms-word');
header('Content-Disposition: attachment; Filename='.$myFile);

I'm just having a hard time just fitting the picture together because they both tasks have a 'Content-Type' header. Do i create the file first, save it, then download it? Or can i do it all (create a doc file and have user download it) in one php file?


回答1:


You only need the "headers found to make a word document." The first set are for a generic streaming download.




回答2:


Your second set of headers are fine. No need for the first. The Content-Disposition header is the one that will typically force a download. (Although, you should be aware that clients can do whatever they want with a file, and you have no direct control over this.)

You can create the file and send it straight to the client without saving it to the server's disk, depending on how you are creating this document.



来源:https://stackoverflow.com/questions/7960370/dynamically-create-and-download-doc-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!