使用php

时间:2017-12-14 20:53:39

标签: php ajax

我正在使用Wamp服务器,我将一些文件放在以下目录中:

  

上传/'姓名' '姓' (每个用户)。 (此目录位于Wamp的www文件夹中。)

我在download.php中下载了用于下载的php代码,我想使用ajax将要下载的文件的id发送到download.php

以下是download.php:

<?php
require_once ('./db.php');
session_start();

$arrayResult = array();
$id = $_POST['id'];
$query = "select Name,OwnerId from fichier where Id = ".$id;
$result = $conn->query($query);
$row = mysqli_fetch_assoc($result);
$name = $row['Name'];
$ownerId = $row['OwnerId'];

$query = "select Firstname,Lastname from user where Id = ".$ownerId;
$result = $conn->query($query);
$row = mysqli_fetch_assoc($result);
$firstname = $row['Firstname'];
$lastname = $row['Lastname'];

$path = 'uploads/'.$firstname.' '.$lastname.'/';
$download_file =  $path.$name;
error_log(filesize($download_file));
if(file_exists($download_file))
{
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($download_file).'"');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($download_file));
    flush();
    readfile($download_file); 
    exit;
    //$arrayResult[] = 'Download Success';
}

这是我的js代码:

$('#downloadLink').click(function(){
    var id = $('input:checkbox:checked').attr('id');
    var tobeSent = {
        'id' : id
    };
    $.ajax({
        type:'POST',
        url:'download.php',
        data:tobeSent,
        dataType: 'json',
        success:function(data){
            alert(data[0]);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);
        }
    });
});

当我运行此代码时没有任何反应,我在chrome中遇到以下控制台错误:

  

SyntaxError:位于0的JSON中的意外标记<

非常感谢任何帮助。

0 个答案:

没有答案