增加附件下载功能

This commit is contained in:
wlx 2011-11-07 13:23:40 +00:00
parent 34b097239c
commit cb14556d3d
1 changed files with 92 additions and 5 deletions

View File

@ -11,7 +11,7 @@ class ServiceController extends Zend_Controller_Action
function preDispatch()
{
$this->db=Zend_Registry::get('db');
$this->view->config = Zend_Registry::get('config');
$this->config = Zend_Registry::get('config');
$this->messenger=$this->_helper->getHelper('FlashMessenger');
$this->view->messages = $this->messenger->getMessages();
}
@ -81,7 +81,7 @@ class ServiceController extends Zend_Controller_Action
fpassthru($file);
exit;
} else {
$url=$this->view->config->bigthumb->path.sprintf('%05d',floor(($thumb['gid']+0.1)/100)*100).'-'.sprintf('%05d',ceil(($thumb['gid']+0.1)/100)*100-1)."/".$thumb['gid'];
$url=$this->config->bigthumb->path.sprintf('%05d',floor(($thumb['gid']+0.1)/100)*100).'-'.sprintf('%05d',ceil(($thumb['gid']+0.1)/100)*100-1)."/".$thumb['gid'];
$url.='/public/'.str_replace('_s.','.',$thumb['filename']);
header("Content-Type:image/".$thumb['filetype']);
$file=fopen($url,'r');
@ -168,7 +168,7 @@ class ServiceController extends Zend_Controller_Action
$xml = new DOMDocument;
$xml->loadXML($row->data);
$xsl = new DOMDocument;
$xsl->load($this->view->config->westdc->xsl);
$xsl->load($this->config->westdc->xsl);
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
@ -600,7 +600,7 @@ class ServiceController extends Zend_Controller_Action
}
$files=new files();
$msg = $files -> upload($this->view->config->upload,$_FILES['Filedata'],'reviewatt');
$msg = $files -> upload($this->config->upload,$_FILES['Filedata'],'reviewatt');
if(empty($msg['error']))
{
@ -656,5 +656,92 @@ class ServiceController extends Zend_Controller_Action
$this->_helper->viewRenderer->setNoRender();
echo Zend_Json::encode($files);
}
/*
* 附件下载
*/
function attachAction()
{
$zipuuid = $this->_request->getParam('zip');
$id=(int)$this->_request->getParam('id');
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
if(!empty($zipuuid))
{
$sql=$this->db->quoteInto("select a.*,md.title from mdattach m left join metadata md on m.uuid=md.uuid left join attachments a on m.id=a.id where m.uuid=? order by a.ts_created desc",$zipuuid);
$atts = $this->db->fetchAll($sql);
$title=$atts[0]['title'];
$zip = new ZipArchive();
$url = tempnam($this->config->temp->path,$zipuuid);//创建临时文件
$last_update=strtotime($atts[0]['ts_created']);
if (!file_exists($url) || (filemtime($fn)<$last_update))
{
if( $zip->open($url, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true )
{
throw new Exception("cannot open {$url} for writing.");
}
$updates = array(); //统计被下载的附件ID
foreach ($atts as $k=>$v)
{
if (file_exists($this->config->upload.$v['filename'])) {
$updates[]=$v['id'];
$zip->addFile($this->config->upload.$v['filename'],'/'.basename($v['realname']));
}
}
$zip->close();
}
//更新统计
$ids=join(',',$updates);
$sql = "update attachments set downtimes=downtimes+1 where id in ($ids)";
@$this->db->exec($sql);
//输出下载
$content=file_get_contents($url);
$this->getResponse()->setHeader('Content-Type', 'application/octet-stream')
->setHeader('Content-Disposition','attachment; filename="'.basename($title).'-文档.zip"')
->setHeader('Content-Length', strlen($content))
->setHeader('Content-Type','application/force-download')
->setHeader('Content-Type','application/download')
->setHeader('Content-Type','application/zip')
->setHeader('Content-Description','File Transfer')
->setHeader('Content-Transfer-Encoding','binary')
->setHeader('Expires',0)
->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0')
->setHeader('Pragma','public')
->setBody($content);
}
elseif ($id>0)
{
//下载单个附件
$sql=$this->db->quoteInto("select a.*,md.title from mdattach m left join metadata md on m.uuid=md.uuid left join attachments a on m.id=a.id where m.id=? order by a.ts_created desc",$id);
$atts = $this->db->fetchRow($sql);
$title=$atts['title'];
$updates = array(); //统计被下载的附件ID
//更新统计
$sql = "update attachments set downtimes=downtimes+1 where id in ($id)";
@$this->db->exec($sql);
//输出下载
$content=file_get_contents($this->config->upload.$atts['filename']);
$this->getResponse()->setHeader('Content-Type', 'application/octet-stream')
->setHeader('Content-Disposition','attachment; filename="'.$atts['realname'].'"')
->setHeader('Content-Length', strlen($content))
->setHeader('Content-Type','application/force-download')
->setHeader('Content-Type','application/download')
//->setHeader('Content-Type','application/zip')
->setHeader('Content-Description','File Transfer')
->setHeader('Content-Transfer-Encoding','binary')
->setHeader('Expires',0)
->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0')
->setHeader('Pragma','public')
->setBody($content);
}
else
{
throw new Exception('发生严重意外!您确认链接正确?');
}
}
}