2

Hi I have into database a list of people,for every people I have associated a file, it can be .pdf or .doc. I have saved into database pdf or doc how longblob. Now, i want to select all people and their file (file column is CV). I do so:

if ($result->num_rows > 0) {

foreach ($result as $item) {

    $dateTmp = explode("-",$item['ETA']);
    $dt = $dateTmp[2]."/".$dateTmp[1]."/".$dateTmp[0];
    $content=base64_encode($item['CV']);


    $array[$index] = '<tr><td>' . $item['NOME'] . '</td><td>' . $item['COGNOME'] . '</td>
        <td>' .$dt.'</td><td>' . $item['TITOLO'] .'</td><td> + </td><span>
        <td>'.$item['EMAIL'].'</td><td>'.$item['TELEFONO'].'</td>
        <td>'.$item['RUOLO'].'</td><td>'.$item['SEDE'].'</td>
        <td><object data="'.$a.'" type="application/pdf">
        </object></td>
        </span></tr>';

    $index++;

}
}

After cycle i have done:

echo json_encode($array);

Then on success of Ajax call, i have done so:

success: function (data) {

                    var result = data;
                    console.log(result);
                    $("#tableFilter tbody").append(result);


                },

This method don't work why? Where i'm wrong? Is rigth put file into element object?

I have solved so: I have changed the code above with this:

$array[$index] = '<tr><td>' . $item['NOME'] . '</td><td>' . $item['COGNOME'] . '</td>
            <td>' .$dt.'</td><td>' . $item['TITOLO'] .'</td><td> + </td><span>
            <td>'.$item['EMAIL'].'</td><td>'.$item['TELEFONO'].'</td>
            <td>'.$item['RUOLO'].'</td><td>'.$item['SEDE'].'</td>
            <td><a href="download.php?id='.$item["ID"].'"  title="DOWNLOAD ">DOWNLOAD FILE</a></td>
            </span></tr>';

Now i create a rowq with link into td, on href i add php page for download file, I also specify into href the id of file. Then if i click on link i call download.php? where the code is:

<?php

$id = $_GET['id'];
$servername = "localhost";
$username = "test";
$password = "mypassword";
$dbname = "mydb";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT BLOBFILE, NAMEFILE,TYPEFILE, SIZEFILE FROM candidati WHERE id = $id";
$result = $conn->query($sql);
$type;
$size;
$nameFile;
$file;

foreach ($result as $item){

    $type = $item["TYPEFILE"];
    $size = $item["SIZEFILE"];
    $nameCv = $item["NAMEFILE"];
    $cv = $item["BLOBFILE"];
}

header("Content-type: ".$type);
header("Content-length: ".$size);
header("Content-Disposition: attachment; filename=".$nameFile);
header("Content-Description: PHP Generated Data");
echo $file;

?> 

And then start download of file. I have a last question. Since i have only one row is possible remove cycle foreach? How?

Fra87
  • 297
  • 2
  • 5
  • 16
  • Possible duplicate of [How do I render a Word document (.doc, .docx) in the browser using JavaScript?](https://stackoverflow.com/questions/27957766/how-do-i-render-a-word-document-doc-docx-in-the-browser-using-javascript) – Peter M Oct 27 '17 at 14:59
  • I use php not javascritp – Fra87 Oct 27 '17 at 15:39
  • Take a look at the answer in the question, they show you how to embed/display the files. – Peter M Oct 27 '17 at 15:41

0 Answers0