Codeigniter:下载文件并重定向

3

我在这个页面上有一个表单:http://www.obsia.com/products/thassos_wonder_brochure/ 当点击提交按钮时,我希望它开始下载文件并重定向到感谢页面。但我只能让它下载文件或重定向或加载感谢下载页面。

这是用于检查表单和提交的函数:

function thassos_wonder_brochure()
{
    $this->load->helper('form');
    $this->load->helper('email');
    $this->load->library('email');
    $this->load->library('form_validation');

    //form validation
    $this->form_validation->set_rules('firstname', 'First Name', 'required|max_length[32]');
    $this->form_validation->set_rules('lastname', 'Last Name', 'required|max_length[32]');
    $this->form_validation->set_rules('companyemail', 'Company Email', 'required|max_length[100]|valid_email|unique');

    $this->load->model('brochure_model');
    if ($this->form_validation->run() == FALSE) 
    {
        // redisplay user form and repopulate fields
        $this->load->view('/tw/thassos_wonder_brochure_view');
    }   
    //display confirmation web page and send email
    else
    {   

        if($query = $this->brochure_model->addContact())
        {
            redirect('/thankyou/thassos_wonder_download');  (UPDATE)    
        }   
    }

}

以下是代码,位于感谢页面的结尾,重定向到下载页面:(更新)
    <?php $this->load->view('header_view.php'); ?>
    <div class="prod">
    <?php $this->load->view('pmenu_view');?>

<div class="product_desc" id="tw_download">
    <h1>Thank you for Downloading</h1>
    <p>You have downloaded the brochure for ThassosWonder - for white stones specifically. You will also receive further information and link to the downloads in your email after you click on the confirmation link in your email.</p>
    <?php echo br(1); ?>
    <p>Feel free to write to us, if you are unable <?php echo anchor('contact', 'to get desired finish from your white marble or stones')?>. You can upload a picture to get a thorough analysis from our Research Director.</p>
    <br />

</div>
    </div>
    <?php $this->load->view('footer_view');?>  
<?php redirect('download/thassos_wonder_brochure'); ?>

这是我在下载页面使用的代码。如何下载文件并将其重定向到感谢页面?
<?php 
$this->load->helper('download');
$name = 'ThassosWonder.pdf';
$data = file_get_contents("./downloads/brochures/Thassos_Wonder_Obsia.pdf"); // Read the file's contents - cannot use relative path. It will try to call the file from same directory (controller) as this file. In order for it get the contents from the root folder put a . in front of the relative path//
force_download($name, $data); 
 ?>

任何帮助将不胜感激。
1个回答

3

换一种方式 - 重定向到“感谢您”页面,然后立即重定向到“强制下载”页面。这将启动下载但让用户留在“感谢您”页面。

更新: 尝试更改

<?php redirect('download/thassos_wonder_brochure'); ?>

为了

<?php redirect('download/thassos_wonder_brochure', 'refresh'); ?>

代替之。

这肯定是自然的做法。 - Wesley Murch
如果我这样做 `if($query = $this->brochure_model->addContact()) { redirect('/thankyou/download'); redirect('/download/thassos_wonder_brochure'); }` 那么它会加载感谢页面,但不会下载。如果我在所有感谢页面的代码末尾添加重定向,则只会强制下载,而不会将其重定向到感谢页面。 - strangeloops
你不能在同一页中进行重定向。你必须重定向到“感谢”页面,然后让该页面重定向到下载页面。 - Repox
我尝试重定向到“感谢”页面,并在我的“加载footerview”代码之后将其重定向到下载页面,在这种情况下,它跳过了前往感谢页面并开始下载的步骤。这里有什么我做错了吗? - strangeloops
你能否更新一下你的问题代码,让我们了解一下你目前的进展? - Repox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接