gpt4 book ai didi

javascript - 如何编写一个 (php) Web 应用程序以向客户端发送一个 .txt 文件,其中包含客户端已插入 JS 提示框的值

转载 作者:行者123 更新时间:2023-11-28 04:58:21 25 4
gpt4 key购买 nike

我希望开发一个(玩具)应用程序,1) 让用户在 JS 提示框中填写几个值,将它们写入 .txt 文件中,2) 将后者发送回用户.

我可以做其中任何一个,但不能将它们组合在一个应用程序中。

我有这两个简短的脚本:

main.html

<html>
<head>
<script type="text/javascript">
function myFunction() {
var person = prompt("Please enter your name", "Harry Potter");
var mail = prompt("Please enter your email", "Harry Potter");
document.getElementById("person").value = person;
document.getElementById("mail").value = mail;
document.getElementById("form").submit(); <<<< NOTE
}

注意,提交似乎会导致 readfile() 失败;下面

 </script>
</head>
<body>

<form id="form" action="minor.php?file=abc.txt" method="post">
<input type="hidden" name="person" id="person" />
<input type="hidden" name="mail" id="mail" />
</form>

<a onclick=myFunction(); href="minor.php?file=abc.txt">see abc.txt</a>';
</body>
</html>

注意我尝试了上面的 action="minor.php?file=abc.txt"href="minor.php?file=abc.txt" (不是同时两者!),并且无论哪种方式都缺少一些东西(请参阅下面我得到的不同输出)

次要.php

<?php  

$file_directory = "Files";
$file = $_GET['file'];
$file_array = explode('/', $file);
file_array_count = count($file_array);
$filename = $file_array[$file_array_count-1];
$file_path = dirname(__FILE__).'/'.$file_directory.'/'.$file;

if (is_writable($file_path)) {
if (!$handle = fopen($file_path, 'a')) {
echo "Cannot open file ($file_path)";
exit;
}

foreach($_POST as $key => $req){ // or use $_REQUEST
if (fwrite($handle, $key."--".$req."\n")== FALSE){
echo "Cannot write to file ($file_path)";
exit;
}
}
fclose($handle);

} else {
echo "The file $file_path is not writable";
}

if(file_exists($file_path)) {
header('Content-type: text/plain');
header("Content-disposition: attachment; filename={$filename}");
readfile($file_path); //Read and stream the file
}
else {
echo "Sorry, the file does not exist!";
}
?>

这是当我打乱几个数据时得到的不同输出:

case A
document.getElementById("form").submit(); included
action="" (empty)
href="minor.php?file=abc.txt"
----
DO download abc.txt
Nothing written into abc.txt
access.log: "GET .../minor.php?file=abc.txt HTTP/1.1" 200 13

case B
//document.getElementById("form").submit(); // Commented out
action="" (empty)
href="minor.php?file=abc.txt"
-----
DO download abc.txt
Nothing written into abc.txt (clear why)
access.log: "GET .../minor.php?file=abc.txt HTTP/1.1" 200

case C
document.getElementById("form").submit(); included
action="minor.php?file=abc.txt"
href="" (empty)
-----
DOES NOT download abc.txt
access.log "GET .../main.html HTTP/1.1" 304 (error??)

欢迎您的帮助。

最佳答案

以下代码满足我的要求:

(minor.php 如上所述,但使用 foreach($_GET ... ) 而不是 foreach($_POST ... ) )

ma​​in.html

<html>
<head>
<script type="text/javascript">
function myFunction() {
var person = prompt("Please enter your name", "Harry Potter");
var mail = prompt("Please enter your email", "Harry Potter");
document.getElementById("clickhere").innerHTML="";
document.getElementById("mainlink").innerHTML="please download now";
document.getElementById("mainlink").href="minor.php?file=abc.txt&"+"person="+person+"&mail="+mail+"";
document.getElementById('mainlink').click();
}
</script>
</head>
<body>
<div onclick=myFunction(); id="clickhere">click here</div>
<a href="#" id="mainlink"></a>
</body>
</html>

我欢迎其他带有改进/替代代码的答案。

关于javascript - 如何编写一个 (php) Web 应用程序以向客户端发送一个 .txt 文件,其中包含客户端已插入 JS 提示框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42349872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com