Hi,
I would like to create a simple form for uploading and downloading documents (like pdf's and doc's) directly to/from the database.
(I have decided to store the files directly in the database due to maintenance and safety reasons but that's another discussion)
I have two tables: The first one contains editable info for the user and the second one contains the files themselves.
These are the tables:
Table 1: files_header Table 2: files_content
+----+--------------+-----------+---------+ +----+-----------------+-------+------+---------+
| id | file_name | file_desc | file_id | | id | name | size | type | content |
+----+--------------+-----------+---------+ +----+-----------------+-------+------+---------+
| 1 | Contract X | Important | 1 | | 1 | contract.pdf | 4532 | pdf | 12e1l.. |
| 2 | Appendix Y | | 4 | | 4 | appendix.doc | 9408 | doc | jk34j.. |
| 3 | Evidence Z | | 9 | | 9 | Screenshot.png | 2054 | png | 90sdc.. |
+----+--------------+-----------+---------+ +----+-----------------+-------+------+---------+
The content column in the second table contains the files themselves in a blob format.
This is my html:
<table id="fileUpload" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>File</th>
</tr>
</thead>
</table>
This is my Script:
editor = new $.fn.dataTable.Editor( {
ajax: "php/control.php",
table: "#fileUpload",
fields: [ {
label: "Name:",
name: "file_name"
}, {
label: "Description:",
name: "file_desc"
}, {
label: "Document:",
name: "file_id",
type: "upload",
display: function ( file_id ) {
return " ????? ";
},
clearText: "Clear"
}
]
} );
var table = $("#fileUpload").DataTable( {
dom: "Brtip",
ajax: "php/control.php",
columns: [
{ data: "file_name" },
{ data: "file_desc" },
{
data: "File_IntID",
render: function ( File_IntID ) {
return ID ?
" ????? " :
null;
},
defaultContent: "No document",
title: "Document"
}
],
select: true,
buttons: [
{ extend: "create", editor: editor },
{ extend: "edit", editor: editor },
{ extend: "remove", editor: editor }
]
} );
In this javascript we can already see my challenge (the question marks "?????"). I would like get the file as a download link in the table.
I have no idea how get this work with DataTables.
But let me show the server side script first.
This is my PHP:
include( "../DataTables/Editor-PHP-1.7.0/php/DataTables.php" );
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
DataTables\Editor\ValidateOptions;
Editor::inst( $db, 'files_header', 'File_UniqueID' )
->fields(
Field::inst( 'id' ),
Field::inst( 'file_name' ),
Field::inst( 'file_desc' ),
Field::inst( 'file_id' )
->setFormatter( Format::ifEmpty( null ) )
->upload( Upload::inst( ????? )
->db( 'files_content', 'id', array(
'name' => Upload::DB_FILE_NAME,
'size' => Upload::DB_FILE_SIZE,
'type' => Upload::DB_MIME_TYPE,
'content' => Upload::DB_CONTENT
) )
->validator( Validate::fileSize( 100000000, 'Files must be smaller than 100MB' ) )
->validator( Validate::fileExtensions( array( 'pdf', 'doc', 'ppt', '...' ), "Please upload a document" ) )
)
)
->process( $_POST )
->json();
Again, I have no idea how to pass something from the PHP to the JS in order to create a download link or at least some functionality to retrieve the file.
I have searched the net for days and tried a lot by myself but couldn't find an answer.
It would be awesome if somebody has an answer to this.
Thank you very much!