0

管理者がアップロードしたjpg、png、pdf、docxファイルをダウンロードできるWebサイトを作成しようとしています。

これらのコンテンツのアップロードは問題なく機能しています。アップロード時に、そのファイル名を mysql テーブルにも挿入します。そのテーブルを使用して、アップロードされたファイル名をユーザーに表示しています。

これは、これらのファイルを表示するために使用しているコードです。

<?php
            if (!empty($downloads)) {
                foreach ($downloads as $val) {
                    ?>            <div class="col-md-3 col-sm-6 col-xs-12">
                        <div class="panel panel-danger">
                            <div class="panel-heading panel-title">
                                <?php echo $val['upload_description']; ?>
                            </div>
                            <div class="panel-body">
                                <?php echo $val['file_name']; ?>
                                <div class="clear-fix clear clearfix"></div>
                                <div id="download" onclick="downloadFile('<?php echo $val['file_name']; ?>');" class="btn btn-danger">Download</div>
                            </div>
                        </div>
                    </div>
                    <?php
                }
            }
            ?>

これは私のajaxコードです

function downloadFile(str) {
                        //alert(str);
                        $.ajax({
                            type: 'POST',
                            url: 'downloadfiles',
                            data: {value: str},
                            success: function(data) {
                                alert('ok' + data);
                            }
                        });
                    }

ダウンロードボタンをクリックすると、ajaxとjqueryを使用して、そのファイル名をdownload_controllerに送信します。これは download_controller ファイルの関数です

public function download_files() {
    $fileName = $this->input->post('value');
    $file_path = 'uploaded/' . $fileName;
    $this->_push_file($file_path, $fileName);
 }

function _push_file($path, $name) {
        $this->load->helper('download');
        // make sure it's a file before doing anything!
        if (is_file($path)) {
            // required for IE
            if (ini_get('zlib.output_compression')) {
                ini_set('zlib.output_compression', 'Off');
            }

            // get the file mime type using the file extension
            $this->load->helper('file');
            $mime = get_mime_by_extension($path);
            // Build the headers to push out the file properly.
            header('Pragma: public');     // required
            header('Expires: 0');         // no cache
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
            header('Cache-Control: private', false);
            header('Content-Type: ' . $mime);  // Add the mime type from Code igniter.
            header('Content-Disposition: attachment; filename="' . basename($name) . '"');  // Add the file name
            header('Content-Transfer-Encoding: binary');
            header('Content-Length: ' . filesize($path)); // provide file size
            header('Connection: close');
            readfile($path); // push it out
            exit();
        }
    }

しかし、ダウンロードは開始されません。PNGファイルをダウンロードしようとすると、この種のメッセージが表示されます。

ここに画像の説明を入力

誰かが私がここでやっている間違ったことを教えてもらえますか?

ありがとうございました

4

1 に答える 1