
谁做过ExtJS上传下载文件的功能,求教 30
2013-07-03 · 知道合伙人软件行家
关注

展开全部
//附件上传表单
var wareFrom = Ext.create('Ext.form.Panel', {
items: [{
xtype: 'filefield',
name: 'upfile',
fieldLabel: '文件上传',
blankText: '请选择文件',
allowBlank: false
}]
});
//创建一个窗体
var win = Ext.create('Ext.window.Window', {
title: '上传课件',
width: 'auto',
height: 'auto',
layout: 'fit',
items: wareFrom,
buttonAlign: 'center',
buttons: [{
minWidth: 80,
text: '取消',
handler: function(){ win.hide(); }
},{
minWidth: 80,
text: '上传',
handler: upLoad
}]
});
//显示窗体
win.show();
//点击上传按钮处理事件
function upLoad(){
if(wareFrom.getForm().isValid()){
wareFrom.getForm().submit({
waitTitle: '请稍候',
waitMsg: '正在执行操作...',
url: 'upload.php?upload=ok',
method: 'POST',
success: function(form, action){
Ext.Msg.alert('提示', action.result.msg);
wareFrom.getForm().reset();
},
failure: function(form,action){
Ext.Msg.alert('提示', action.result.msg);
}
});
}
}
//后台不管你用的什么,流程一致,此以php为例,因为比较好写
<?php
if($_GET['upload']=='ok'){
//上传路径
$location = 'upload_file/';
//此处的name是上传窗体,upload控件的name
if(move_uploaded_file($_FILES['upfile']['tmp_name'], $location)){
echo json_encode(array('success'=>true, 'msg'=>'上传成功'));
}else{
echo json_encode(array('success'=>false, 'msg'=>'上传发生了错误'));
}
}
?>
//下载
<?php
//此处需前台传一个id过来
$id=$_GET['id'];
$sql="SELECT * FROM `ware` WHERE `id`='$id'";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
//此处的row是文件保存在数据库的路径
if(file_exists($row[0])){
//用stream读取该文件
$file = fopen($row[0], 'r');
header('Content-Type: application/octet-stream');
header('Accept-Ranges: bytes');
header('Accept-Length: '.filesize($row[0]));
//此处的row1是文件名称
header('Content-Disposition: attachment; filename='.$row[1]);
echo fread($file,filesize($row[0]));
fclose($file);
}
?>
//有什么地方不明白的话,欢迎继续追问
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询