修改了数据附件上传的方式,支持多附件上传

This commit is contained in:
Li Jianxuan 2011-11-07 08:34:13 +00:00
parent 806dbcf49a
commit 5f18fd4259
2 changed files with 126 additions and 73 deletions

View File

@ -1090,46 +1090,12 @@ class Admin_DataController extends Zend_Controller_Action
$this->view->mdtitle=$mdtitle;
}
if($add && empty($edit))
if($add)
{
if(empty($submit))
$this->_helper->viewRenderer('attachmentsadd');
else
if(!empty($uuid))
{
$files=new files();
$msg = $files -> upload($this->view->config->upload,$_FILES['Filedata'],$_POST['dir']);
if(empty($msg['error']))
{
$filename = $msg['db_path'];
$filesize = $msg['file_size'];
$filedesc = $this->_request->getParam('filedesc');
$filetype = $this->_request->getParam('dir');
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$user = $auth->getIdentity();
$userid = $user->id;
$sql = "insert into attachments (filename,filetype,filedesc,userid,filesize) values ('$filename','$filetype','$filedesc','$userid','$filesize')";
if($this->db->exec($sql)>0)
{
$this->messenger->addMessage('附件上传成功!');
$this->_redirect("/admin/data/attachments/add/1");
}
}
else
{
$this->messenger->addMessage('读取用户信息出错');
@unlink($filename);
$this->_redirect("/admin/data/attachments/add/1");
}
}else
{
$this->messenger->addMessage('附件上传失败:'.$msg['error']);
@unlink($filename);
$this->_redirect("/admin/data/attachments/add/1");
}
$this->view->uuid = $uuid;
}
}//附件添加
else if($delete)
@ -1311,6 +1277,73 @@ class Admin_DataController extends Zend_Controller_Action
}//attachments 附件
function uploadAction(){
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$uuid = $this->_getParam('uuid');
try{
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$user = $auth->getIdentity();
$userid = $user->id;
}
if(empty($userid)||!is_numeric($userid)){
$msg['error'] = "请先登录";
echo Zend_Json::encode($msg);
exit();
}
$files=new files();
$msg = $files -> upload($this->view->config->upload,$_FILES['Filedata'],'md');
if(empty($msg['error']))
{
$msg['error']="";
$filename = $msg['db_path'];
$filesize = $msg['file_size'];
$filedesc = $this->_request->getParam('filedesc');
$filetype = $this->_request->getParam('dir');
$realname = $msg['realname'];
$sql = "insert into attachments (filename,filetype,filedesc,userid,filesize,realname) values ('$filename','reviewatt','$filedesc','$userid','$filesize','$realname') RETURNING id";
$sth = $this->db->prepare($sql);
$sth->execute();
$att = $sth->fetch(PDO::FETCH_ASSOC);
$msg['attid'] = $attid = $att['id'];
$sql = "insert into mdattach (uuid,id) values ('$uuid','$attid')";
if($this->db->exec($sql))
{
$msg['html'] = $realname.'[已完成]<input type="hidden" name="atts[]" value="'.$attid.'" /><div class="cancel"><a href="javascript:;" id="deletebtn_'.$attid.'"><img border="0" src="/static/js/uploadify/cancel.png" /></a></div>';
echo Zend_Json::encode($msg);
exit();
}else{
$msg['error'] = '附件上传失败:写入附件表出错';
@unlink($filename);
echo Zend_Json::encode($msg);
exit();
}
}else{
$msg['error'] = '附件上传失败:'.$msg['error'];
@unlink($filename);
echo Zend_Json::encode($msg);
exit();
}
}catch(Exception $e){
$msg['error'] = "错误:".$e->getMessage();
echo Zend_Json::encode($msg);
exit();
}
}// uploadAction ajax上传附件
/*
获得单个文件的信息
return array row

View File

@ -3,15 +3,14 @@
$this->headTitle('后台管理');
$this->headTitle()->setSeparator(' - ');
$this->headLink()->appendStylesheet('/css/admin.css');
$this->headLink()->appendStylesheet('/js/swfupload/swfupload.css');
$this->breadcrumb('<a href="/">首页</a>');
$this->breadcrumb('<a href="/admin">后台首页</a>');
$this->breadcrumb('数据管理');
$this->breadcrumb()->setSeparator(' > ');
$this->headScript()->appendFile('/js/swfupload/swfupload.js');
$this->headScript()->appendFile('/js/swfupload/handlers.js');
$this->headScript()->appendFile('/js/swfupload/fileprogress.js');
$this->headLink()->appendStylesheet('/static/js/uploadify/uploadify.css');
$this->headScript()->appendFile('/js/jquery-1.6.4.min.js');
$this->headScript()->appendFile('/static/js/uploadify/swfobject.js');
$this->headScript()->appendFile('/static/js/uploadify/jquery.uploadify.v2.1.4.min.js');
?>
<div id="leftPanel">
<?= $this->partial('data/left.phtml'); ?>
@ -30,36 +29,57 @@
<form action="#" method="POST" enctype='multipart/form-data'>
<input type="hidden" name="add" value="1" />
<input type="hidden" name="submit" value="1" />
<input type="hidden" name="edit" value="<?php echo $this->info['id'];?>" />
<table>
<tr>
<td width="100">文件描述</td>
<td><textarea name="filedesc" style="width:300px;"><?php echo $this->info['filedesc'];?></textarea></td>
</tr>
<tr>
<td>文件类型</td>
<td>
<select name="dir" style="width:100px;" <?php if(!empty($this->info['filename'])) echo 'disabled="disabled"';?>>
<option value="image" <?php if($this->info['filetype']=='image') echo 'selected="selected"'; ?>>图片</option>
<option value="file" <?php if($this->info['filetype']=='file') echo 'selected="selected"'; ?>>文件</option>
<option value="media" <?php if($this->info['filetype']=='media') echo 'selected="selected"'; ?>>媒体</option>
</select>
</td>
</tr>
<tr>
<td>文件</td>
<td>
<?php if(empty($this->info['filename']))
echo '<input type="file" name="Filedata" id="file" />';
else
echo '<input style="width:300px;" type="text" readonly="readonly" value="'.$this->info['filename'].'" />';
?>
</td>
</tr>
</table>
<input type="submit" name="submit" value="<?php if(empty($this->info['filename'])) echo "上传";else echo "修改";?>" />
<p>注:可选择多个文件<br />
<ul id="datalist"></ul>
<input id="file_upload" name="Filedata" type="file" />
<input type="button" class="btn btn-small" onclick="$('#file_upload').uploadifyUpload();" value="上传" />
</p>
</form>
</div>
</div>
<script>
$(document).ready(function() {
$('#file_upload').uploadify({
'uploader' : '/static/js/uploadify/uploadify.swf',
'scriptData': {'PHPSESSID' : '<?php echo session_id(); ?>'},
'script' : '/admin/data/upload/<?php if(!empty($this->uuid)) echo "uuid/".$this->uuid; ?>',
'cancelImg' : '/static/js/uploadify/cancel.png',
'folder' : '/uploads',
'multi' : true,
'auto' : false,
'displayData' : 'speed',
'buttonImg' : '/static/js/uploadify/selectfile.gif',
'onComplete' : function(event, ID, fileObj, response, data) {
var obj = jQuery.parseJSON(response);
var html;
if(obj.error=='')
{html = obj.html;}else{html = obj.error;}
$('<li/>', {
"id":'uploadedItem_'+obj.attid,
"class":'uploadifyQueueItem',
"html": html
}).appendTo('#datalist');
$('#deletebtn_'+obj.attid).bind('click', function() {
deleteatt(obj.attid);
});
},
'onError' : function (event,ID,fileObj,errorObj) {
alert(errorObj.type + ' Error: ' + errorObj.info);
}
});
function deleteatt(attid){
$.ajax({
type:"POST",
url:'/upload/delreviewatt/id/'+attid,
data:'',
success:function(html){
$('#uploadedItem_'+attid).remove();
},
beforeSend:function(){
$('#deletebtn_'+attid).html('<img src="/images/11887177066.gif" />');
}
});
}
});
</script>