简体   繁体   English

将文件下载到特定文件夹中

[英]download file in a specific folder

i have a link that allows me to downlowd a text file in my web page but the problem is that i want the user to be able to chose where to save the file i mean when he clicks the link a window should be opened so he can save the file wherever he likes can any one tell me how to do that? 我有一个链接,可以让我在网页中下载文本文件,但问题是我希望用户能够选择将文件保存在哪里,我的意思是当他单击链接时应打开一个窗口,以便他可以将文件保存到他喜欢的任何地方,谁能告诉我该怎么做? thx . 谢谢 。 here is a part of my code: 这是我的代码的一部分:

$fichierres=fopen('res.txt','a');
ftruncate($fichierres,0); 
...
fputs($fichierres, $t."\r\n");
...
fclose($fichierres);
echo'   <div style="text-align:center"><br>  <button id="download" width="100px" class="styled-button-8"><a href="res.txt" download="res.txt" style="color: #FFFFFF"><b>Download</b></a></button></div><br>';

Most browsers will auto-open any file that they can read - exactly how they should work. 大多数浏览器会自动打开它们可以读取的任何文件-确切地说它们应该如何工作。 This includes .txt files, there's nothing that you can do to prevent this. 这包括.txt文件,您无法采取任何措施来防止这种情况。

What you can do is provide the link as an anchor ( <a href="/myfile.txt">Download</a> ) and provide a message next to the link telling the user to "Right click / Save link as..." to download - this will allow them to save the file rather than download. 您可以做的是将链接作为锚点( <a href="/myfile.txt">Download</a> ),并在链接旁边提供一条消息,告诉用户“右键单击/另存为。”。 。”下载-这将使他们可以保存文件而不是下载。

The exact option in the right click menu will differ between browsers but it's always something along the lines of "Save Link As...". 右键菜单中的确切选项在不同的浏览器中会有所不同,但这始终与“将链接另存为...”类似。

http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html

19.5.1 Content-Disposition 19.5.1内容处置

The Content-Disposition response-header field has been proposed as a means for the origin server to suggest a default filename if the user requests that the content is saved to a file. 已经提出了Content-Disposition响应标头字段,作为原始服务器在用户请求将内容保存到文件时建议默认文件名的一种方式。 This usage is derived from the definition of Content-Disposition in RFC 1806 [35]. 这种用法源自RFC 1806 [35]中Content-Disposition的定义。

 content-disposition = "Content-Disposition" ":" disposition-type *( ";" disposition-parm ) disposition-type = "attachment" | disp-extension-token disposition-parm = filename-parm | disp-extension-parm filename-parm = "filename" "=" quoted-string disp-extension-token = token disp-extension-parm = token "=" ( token | quoted-string ) 

An example is 一个例子是

 Content-Disposition: attachment; filename="fname.ext" 

In PHP, you would use header function to send this header. 在PHP中,您可以使用header函数发送此标头。 Note that this must be called before any data is sent. 请注意,必须在发送任何数据之前调用此方法。

header('Content-Disposition: attachment; filename="fname.ext"');

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

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