file()

Since: Editor 1.5

Get information about an uploaded file.
Please note - this property requires the Editor extension for DataTables.

Description

The Editor upload and uploadMany field types provide the ability for an end user to upload files to a server, potentially storing information about the file on a database (this is not mandatory for the upload field types to work, but it is required for this method to return information about the file).

Uploaded files are represented as a single value (typically a database id column), which doesn't always provide the information that is required for display to the end user (for example file name, size, type, etc). This method provides the ability to get the information that has been stored in a database about a file.

The information that is available about each file is directly determined by how the db() method has been configured for the Upload class on the server-side and will automatically populate the data source used for this method.

Please see the PHP and .NET for details of how to configure the db() method. If db() is not used to store file information on a database, this method will always give an error.

Type

file( table, id )

Get information about an uploaded file that has been uploaded through Editor (upload or uploadMany). The information returned is determined by how the db() method has been configured for the Upload class on the server-side (if this has been used). Please see the PHP and .NET for details on this method.

Parameters:

Returns:

object:
Information about the selected file. An error will be thrown if either the table or the file identifier in the table is not found.

Example

Use file() to display an image in a DataTable column and the Editor form:

$(document).ready(function () {
	var editor = new DataTable.Editor({
		ajax: '../php/upload.php',
		table: '#example',
		fields: [
			{
				label: 'First name:',
				name: 'users.first_name'
			},
			{
				label: 'Last name:',
				name: 'users.last_name'
			},
			{
				label: 'Image:',
				name: 'users_files.fileId',
				type: 'upload',
				display: function (fileId) {
					return '<img src="' + editor.file('files', fileId).web_path + '"/>';
				}
			}
		]
	});

	var table = new DataTable('#myTable', {
		ajax: '../php/upload.php',
		columns: [
			{ data: 'users.first_name' },
			{ data: 'users.last_name' },
			{ data: 'users.phone' },
			{ data: 'sites.name' },
			{
				data: 'users_files.fileId',
				render: function (fileId) {
					return fileId
						? '<img src="' + editor.file('files', fileId).web_path + '"/>'
						: 'No image';
				}
			}
		],
		layout: {
			topStart: {
				buttons: [
					{ extend: 'create', editor: editor },
					{ extend: 'edit', editor: editor },
					{ extend: 'remove', editor: editor }
				]
			}
		},
		select: true
	});
});

Related

The following options are directly related and may also be useful in your application development.