File Upload and Download
Posted: Tue Jan 06, 2015 1:55 pm
I want to be able to upload and download pdf files stored as BLOB in the db (or an upload directory on the webserver), not an external file service.
One file should be linked to one record.
The elegant solution here is for Nubuilder 2, but Nubuilder 3 has deserted the File object:
http://forums.nubuilder.cloud/viewtopic.p ... ile#p11460
The upload part works fine when using this on After Save of the upload form:
I have looked in the php code FILE, nugetfile.php and here:
http://wiki.nubuilder.net/index.php/Mis ... pload_Form
http://wiki.nubuilder.com/tiki-index.ph ... uilderDocs
but I can not figure out how to solve the download part and linking to the record.
I know I need to set the header like in this code:
Any help is appreciated.
Tinka
One file should be linked to one record.
The elegant solution here is for Nubuilder 2, but Nubuilder 3 has deserted the File object:
http://forums.nubuilder.cloud/viewtopic.p ... ile#p11460
The upload part works fine when using this on After Save of the upload form:
Code: Select all
$J = json_decode("#FILES#");
if($J[0]->name == ''){return;}
$dt = new DateTime();
$date = $dt->format('Y-m-d H:i:s');
$name = $J[0]->name;
$type = $J[0]->type;
$size = $J[0]->size;
$uploaddir = sys_get_temp_dir();
$contents = file_get_contents($uploaddir . '/' . $J[0]->name);
$contents = addslashes($contents);
$s = "
UPDATE zzzsys_file
SET
sfi_blob = '$contents',
sfi_name = '$name',
sfi_type = '$type',
sfi_size = '$size',
zzzsys_file_log_changed_at = '$date'
WHERE zzzsys_file_id = '#RECORD_ID#'
";
nuRunQuery($s);
unlink('tmp/' . $J[0]->name);
http://wiki.nubuilder.net/index.php/Mis ... pload_Form
http://wiki.nubuilder.com/tiki-index.ph ... uilderDocs
but I can not figure out how to solve the download part and linking to the record.
I know I need to set the header like in this code:
Code: Select all
/*
* Get record from db using parameter passed through runIt() via hash variables
*/
// Get parameters from hash variables
$tblName = '#runIt1#'; // table name to search in
$fldName = '#runIt2#'; // field name containing BLOB data
$docName = '#runIt3#'; // file to open
// Allowed file extension
// Uncomment all extensions you want to allow download
$allowedExtensions = array(
'txt',
// 'html',
// 'htm',
// 'exe',
'zip',
'doc',
'xls',
// 'ppt',
// 'gif',
// 'png',
// 'jpeg',
// 'jpg',
// 'php',
'pdf'
);
// Check if allowed download (only globeadmin can download all file)
$ext = strtolower(substr(strrchr($docName,"."),1));
if (array_key_exists($ext, $allowedExtensions) || ('#zzsys_user_id#' == 'globeadmin')) {
// Build and execute the query
$sql = "SELECT * FROM $tblName WHERE " . $fldName . "_file_name = '$docName' ";
$row = db_fetch_object(nuRunQuery($sql));
if ($row) {
// Get data from DB
$file = $row->$fldName;
$type = $row->{$fldName . "_file_type"};
header("Content-type: ".$type);
header('Content-Disposition: attachment; filename="'.$docName.'"');
header("Pragma: private");
header("Cache-control: private, must-revalidate");
header("Content-Transfer-Encoding: binary");
// show the document
print $file;
}
Tinka