如何在drupal 7中创建文件下载弹出窗体

时间:2013-05-14 13:15:48

标签: drupal-7 popup download

我是drupal的新手。我想创建一个弹出窗口,其中包含名称,电子邮件字段,下载和关闭按钮,当用户单击弹出窗体要显示的下载链接时,用户在验证后提供名称和电子邮件,文件已由用户下载,还有多少用户已下载我如何点他的

1 个答案:

答案 0 :(得分:2)

您可以使用Webform protected downloads模块,该模块会在下载文件之前提示用户填写至少一个电子邮件字段。您可以添加任意数量的文件。请参阅此回答Fill form before download

如果您想手动执行此操作,请创建包含必填字段的webform,然后您可以使用hook_form_alter。在你的template.php中,

    function YOUR_THEME_form_alter(&$form, &$form_state, $form_id){
        if($form_id == "YOUR_WEBFORM_ID")
        {
            $form['#submit'][] = 'download_file';
        }
    }

    function download_file(){

        $file_path = variable_get('file_public_path', conf_path() . '/files');
        $file_name = $file_path."/YOUR_FILE";
        $file = fopen($file_name, 'r');
        $file_size = filesize($file_name);

        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        header("Content-Type: application/force-download");
        header("Content-Type: application/download");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=\"".  urlencode('FILENAME')."\""); // use 'attachment' to force a download
        header("Content-length: $file_size");
        header("Cache-control: private"); //use this to open files directly
        while(!feof($file)) {
             $buffer = fread($file, 2048);
             echo $buffer;
             flush();
        }
        $url = $_SERVER['REQUEST_URI'];
        header('Location:'. $url);
        fclose ($file);
}

这将在提交表格时下载文件。

要使表单弹出,请在表单设置下将表单作为块提供,并将表单块放在任何jQuery插件中,如thickbox

相关问题