PHP文件下载使用隐藏的iframe不保存下载使用、不保存、文件、PHP

2023-09-10 19:59:43 作者:矫情尼玛逼

我提前为混乱的问题的措辞道歉,但我无法弄清楚如何把这个。

I apologize in advance for the confusing question wording, but I couldn't figure out how to put this.

我基本上有一个字符串,我打算让供用户下载的数据库。我怎么会去这样做呢?

I essentially have a string in a database that I intend to make available for user download. How would I go about doing this?

我试图使用AJAX,但我不知道如何去做。

I was attempting to use ajax, but I wasn't sure how to go about it.

在下载链接是pssed $ P $下面的jQuery code执行

The following jquery code is executed when the download link is pressed

$.ajax({

    url: 'index.php/script/downloadFile',
    type: 'post',
    data: {name: $(this).text()}

});

有关PHP code是这样的:

The relevant PHP code looks like:

public function downloadScript(){

    $name = $_POST['name'];

    $filename = $name . ".txt";
    $string = //String that comes from database using name to create query
    $filename = $name . ".txt";

    header('Content-type: text/plain');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header("Content-Length: " . strlen($string));
    header("Connection: close");

    echo $string;
}

基本上,我希望字符串在那里为一个用户下载为文本文件。我不知道如果AJAX是正确的方式来做到这一点,如果有一个更好的方式做同样的任务。 (我是假设了更好的办法是将纳入一个隐藏的iframe某种程度上)

Essentially, I want the string to be there for a user to download as a text file. I didn't know if AJAX was the right way to do this or if there was a better way to do the same task. (I was assuming the better way would be to incorporate a hidden iframe somehow)

有关多一点信息,我有元素的列表:

For a little more information, I have a list of elements:

约翰· 杰夫 乔 杰克

在一个点击,我想该元素的名称的文本发送到PHP文件,形成查询。

When one is clicked, I want to send the text of that element's name to the php file to form the query.

这就是为什么我没有一个简单的HREF设置开始文件的下载。

That's why I don't have a simple href setup to start the file download.

编辑:

而我在这,反正是有,我得到一个以上的这些文件,说有被从数据库返回多个字符串,它们保存为单独的文本文件,压缩他们,并把他们给用户?或者,就需要保存到服务器?

While I'm at it, is there anyway for me to get more than one of these files, say there are multiple strings that are returned from the database, save them as separate text files, zip them up, and send them to the user? Or, would that require saving to the server?

任何帮助是AP preciated!

Any help is appreciated!

推荐答案

您可以只让一个链接

<ul>
    <li class="click_link">John</li>
    <li class="click_link">Bob</li>
</ul>

<script>
$('li').click(function(e) {
    window.location.href = 'getfile.php?name=' + $(this).text();
});
</script>

然后改变你的 $ _ POST $ _ GET

这将只要下载该文件的标题设置是否正确。

This will download the file as long as your headers are set correctly.