ExtJS + fileuploadfield上传文件代码
后台服务端接收文件的代码:
/**
* 后台上传文件处理Action
*/
@RequestMapping(value = "/uploadFile", method=RequestMethod.POST)
public void uploadFile(@RequestParam(value="file",required=true) MultipartFile file ,HttpServletResponse response) {
ModelMap modelMap = new ModelMap();
String savePath = "D:/tmp/";//保存路径 try {
String fileName = file.getName();
File saveFile = new File(savePath);
if(!saveFile.exists()){
saveFile.mkdirs();
}
saveFile = new File(savePath, fileName);
file.transferTo(saveFile);
modelMap.addAttribute("success", true);
} catch (Exception e) {
modelMap.addAttribute("success", false);
modelMap.addAttribute("message", "保存失败:"+e.getMessage());
} JSONSerializer serializer = new JSONSerializer();
String result = serializer.serialize(modelMap);
//ExtJS上传需要用这种方法实现返回
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.write(result);
writer.flush();
writer.close();
}
刚开始使用 return modelMap 返回信息,但是前台就是接收不到数据,最后看API后使用PrintWriter来搞定。
附上前台上传窗口代码:
UploadForm = Ext.extend(Ext.Window,{
constructor : function(a){
Ext.applyIf(this,a);
this.initUIComponents();
UploadForm.superclass.constructor.call(this,{
layout : 'fit',
modal : true,//遮罩层
constrain : true,
width : 500,
height : 200,
title : '选择上传文件窗口',
items : this.formPanel,
buttonAlign : 'center',
keys : [{
key : Ext.EventObject.ENTER,
scope: this,
fn: this.uploadFile
}],
buttons : [{
text : '保存',
scope : this,
iconCls : "btn-save",
handler: this.uploadFile
},{
text : '取消',
scope : this,
iconCls : "btn-cancel",
handler : function(){
this.close();
}
}]
});
},
initUIComponents : function(){
this.formPanel = new Ext.FormPanel({
layout : 'form',
fileUpload: true,
border : false,
method:'POST',
enctype:'multipart/form-data',
bodyStyle : 'padding: 10px 10px 0 10px;',
url : _ctx + '/fuile/uploadFile.do',
defaults : {
anchor : '100%'
},
labelAlign : 'right',
items : [
{xtype : 'hidden',name : 'userId',value : this.userId},
Ext.Util.buildColumnForm(1,'textfield',{
fieldLabel : '备注信息',
name : 'remark',
allowBlank : false,
maxLength : 100,
maxLengthText : '信息长度小于等于100个字符'
}),
{
xtype: 'fileuploadfield',
id: 'form_file',
fieldLabel : '脚本上传',
name : 'file',//后台接收
emptyText: '请上传word文档',
buttonText: '',
regex : /\.(doc|docx)$/,
regexText : "请上传word文档",
buttonCfg: {
iconCls: 'btn-upload-icon'
}
}
]
});
},
uploadFile : function(){
var win = this;
var formFile = Ext.getCmp('form_file').getValue();
if(this.formPanel.getForm().isValid()){
if(formFile==''){
Ext.Msg.alert("操作提示:", "请上传word文件然后保存");
return;
}
this.formPanel.getForm().submit({
url: ctx + '/file/uploadFile.do',
waitMsg: '正在保存...',
success : function(form, action){
var result = Ext.decode(action.response.responseText);
Ext.Msg.alert(result.message, "");
win.close();
},
failure: function(form, action) {
var result = Ext.decode(action.response.responseText);
Ext.Msg.alert("错误提示", result.message);
}
});
}
}
});
调用方法 new UploadForm({userId : '34567'}).show();
ExtJS + fileuploadfield上传文件代码的更多相关文章
- iOS上传文件代码,自定义组装body
以下代码为上传文件所用代码,简单方便,搞了好久,终于知道这么简单的方式来上传. 其它类库也就是把这几句代码封装的乱七八糟得,让你老久搞不懂原理.不就是在body上面加点字符串,body下面加点字符串, ...
- php 上传文件代码
通过 PHP,能够把文件上传到server.里面加入一些图片的推断,假设不加推断文件的类型就能够上传随意格式的文件. 为了站点的安全,肯定不让上传php文件,假设有人进入你的后台,上传了一个php文件 ...
- java上传文件代码
import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;impo ...
- PHP上传文件代码练习2 (重复文章)
表单: <html> <head> <meta http-equiv="Content-Type" content="text/html; ...
- SpringMvc通过controller上传文件代码示例
上传文件这个功能用的比较多,不难,但是每次写都很别扭.记录在此,以备以后copy用. package com.**.**.**.web.api; import io.swagger.annotatio ...
- php上传文件代码解析
思想:把html的input标签组织成一个数组,然后去重 关键技术涉及的函数 is_dir mkdir move_uploaded_file() 涉及的数组 预定义数组$_FILES 步骤一:检查上传 ...
- git 和码云的上传文件代码操作
Git与Github的连接与使用 一 安装git软件 1.git介绍 ''' git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 分布式相比于集中式的最大区别在于开发 ...
- javaWeb上传文件代码
javaweb两种方式的上传,1普通上传,2:jquery ajax后台上传,部分截图如下: 完成包下载,下载后倒入myeclipse工程即可,下载地址:http://files.cnblogs.co ...
- easyui 上传文件代码
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;usi ...
随机推荐
- bzoj3262: 陌上花开(树套树)
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- dotnet反编译工具大全
反编译不是为了破解软件,而是在开发时更好的读懂程序干了什么,比如MVC的源码,如果使用1和4的VS插件能更好的进行断点跟踪. 常用,效率最高: 1.[.NET Reflector]首选,能比较好的反编 ...
- Android数据存储-文件操作
一.预备知识 1.Android中的MVC设计模式 MVC (Model-View-Controller):M是指逻辑模型,V是指视图模型,C则是控制器.一个逻辑模型可以对于多种视图模型,比如一批统计 ...
- CSS基础知识真难啊-background-渐变
文章参考 http://www.zhangxinxu.com/wordpress/?p=727 http://www.uqu8.com/html/2014/html-css_1105/176.html ...
- log4j属性详解
Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使用这三个组件可以轻 ...
- 数据结构作业——brothers(二叉树)
brothers Description 给你一棵节点编号从 1 到 n 的,根节点为 1 的二叉树.然后有 q 个询问,每个询问给出一个整数表示树的节点,要求这个节点的兄弟节点数目和堂兄弟节点的数目 ...
- django views中提示cannot convert dictionary update sequence element #0 to a sequence错误
def message(request): message_list = MessageBoard.objects.all().order_by('-pk') return render(reques ...
- [转]如何启用Ubuntu的休眠模式
大家都知道 Windows 有休眠模式,其实 Ubuntu 也有.休眠模式简单来说,就是可以在用户暂时离开时将内存中的所有内容都写入到硬盘当中,当用户下次开机时,就可以直接启动到上次保存的时间状态. ...
- mainBundle和CustomBundle
iOS中NSBundle类 An NSBundle object represents a location in the file system that groups code and resou ...
- [USACO 2010 OPEN]SLIED
传送门 这道题的题意描述简直有毒.题没看完一眼分层图,然后火速敲了个堆优化的dijkstra,然后就被样例教做人了QAQ 这里说的最坏的情况让我很迷茫?感觉很难判定到底什么是最坏的情况以及确定了最坏的 ...