Extjs 上传和下载文件
来源:互联网 发布:nba竞猜软件 编辑:程序博客网 时间:2024/05/22 06:47
一、前台JS部分
1、展示上传面板
var uploadPanel = this.uploadPanel = new Ext.FormPanel({
width: 600,
height: 50,
region: 'north',
frame: true,
layout: 'fit',
fileUpload: true,
closeAction:'hide',
buttonAlign: 'center',
layout: 'form',
border:0,
items:[
{
layout: 'form',style:'margin-top:6px;margin-left:30px;',
items:[
{
xtype: 'fileuploadfield',allowBlank:false,blankText:'授权文件必须上传',width: 370,fieldLabel: '上传授权文件',name:'uploadFile',buttonText: '浏览'
}
]
}
]
})
2、提交上次文件函数
savaFile:function(){
var id = this.searchForm.getForm().findField("id").getValue();
Ext.MessageBox.confirm('提示', '确定保存设置 ?', function(btn) {
if(btn != 'yes') {
return;
}
th.menuTree.uploadPanel.form.doAction('submit', {
url: '../upload/savaFile.sdo',
method: 'post',
waitTitle:'请等待',
waitMsg: '正在提交...',
params: {id: id},
timeout: 600000,//十分钟
success: function(form, action) {
if(action.result.success == true) {
Ext.MessageBox.alert('提示', '操作成功!');
th.menuTree.hide();
}
else {
Ext.MessageBox.alert('提示', '操作失败:');
}
}
});
});
}
二、Controller控制层
@RequestMapping("/savaFile.sdo")
public @ResponseBody String savaFile(HttpServletRequest request,HttpServletResponse response,
@RequestParam(value="id", required=false) String Id)throws Exception {
uploadService = (UploadService)SpringHelper.getBean("uploadService ");
JSONObject obj = new JSONObject();
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartRequest.getFile("uploadFile");
uploadService .savaFile(file,Id);
obj.put("success",true);
return obj.toString();
}
三、写到服务器
FileUtil.SaveFile( file.getInputStream(),UploadFile.url,orname);//文件流、要保存的地址、文件名称
/**
* 件文件保存到本地
* stream 文件流
* savaUrl 文件要写入的地址
* @param url
* @throws FileNotFoundException
*/
public static void SaveFile(InputStream stream,String savaUrl,String fileName) throws FileNotFoundException{
try {
isExist(savaUrl); //判断文件夹是否存在,如果不存在则创建文件夹
OutputStream os = new FileOutputStream(new File(savaUrl+"/"+fileName));
int len = 0;
byte[] b = new byte[1024];
while ((len = stream.read(b)) > 0){
os.write(b, 0, len);
}
b = null;
stream.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
四、从服务下载文件JS
uploadFile:function(selectedRows){
var url = this.ctx + '/log/uploadLogFile.sdo?fileName='+encodeURI(encodeURI(selectedRows[0].data.fileUrl));
window.open(url);
},
五、从服务下载文件Controller
/**
* 文件下载
* @param request
* @param response
* @param fileName
*/
@RequestMapping("/uploadLogFile.sdo")
public void doExport(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="fileName", required=false) String fileName) {
OutputStream outputStream = null;
InputStream ins = null;
try {
if(request.getMethod().equals("GET")){
fileName = java.net.URLDecoder.decode(fileName,"UTF-8");
}
String filePath = UploadFile.url+"/"+fileName;
File downFile = new File(filePath);
if(!downFile.exists()) {
AjaxOut.responseText(response, filePath + "[文件不存在]");
return;
}
response.setHeader("Content-Disposition", "attachment;filename=\"" + new String(fileName.getBytes("GBK"),"ISO8859_1") + "\"");
String contentType = new MimetypesFileTypeMap().getContentType(fileName);
response.setContentType(contentType + ";charset=UTF-8");
outputStream = response.getOutputStream();
ins = new FileInputStream(downFile);
byte[] buffer = new byte[2048];
int i = -1;
while ((i = ins.read(buffer)) != -1) {
outputStream.write(buffer, 0, i);
}
outputStream.flush();
outputStream.close();
ins.close();
}
catch (Exception e) {
AjaxOut.responseText(response, "下载文件错误!<br />" + e.getMessage());
}
finally {
try {
if(outputStream != null)
outputStream.close();
if(ins != null)
ins.close();
} catch (IOException e) {
}
}
}
- Extjs 上传和下载文件
- struts2+extjs文件上传,提示下载问题解决
- ExtJS+SpringMVC文件上传与下载
- strust2+extjs文件上传,提示下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- SqlServer2005/2008备份模式与恢复模式 2备份模式
- 排序——归并排序
- FFmpeg获取DirectShow设备数据(摄像头,录屏)
- 关于汉诺塔,C++代码,代码效果演算
- 关于Reporting bugs的一些知识
- Extjs 上传和下载文件
- 【有趣】巧克力香辣牛肉月饼一出,五仁月饼地位不保
- 使用valgrind进行后台服务器的性能优化
- Remove Nth Node From End of List
- ApplicationContextAware接口,可在任何代码任何地方任何时候中取出ApplicaitonContext
- PHP小白遇到的第一个问题Warning: mysql_connect() [function.mysql-connect]
- 我的工作
- 【UVA】10881-Piotr's Ants
- 关于路径的问题 2014-08-18