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 ...
随机推荐
- springMVC-错误消息的显示和国际化
显示:在页面添加<form:errors path="*">会把错误消息集中显示在一块 在页面添加<form:errors path="lastname ...
- UOJ265 【NOIP2016】愤怒的小鸟
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- BZOJ1922 [Sdoi2010]大陆争霸
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- hdu3932 模拟退火
模拟退火绝对是从OI--ACM以来接触过的所有算法里面最黑科技的orz 题意:地上有一堆hole,要找一个点,使得(距离该点最远的hole的距离)最小. sol:本来想套昨天的模拟退火模板,初值(0, ...
- Windows下Nginx+Mysql+Php(wnmp)环境搭建
前言 最近想在windows下使用nginx搭建web环境,本来想用套件(WNMP)一键安装,但后来放弃了,觉得还是自己动手,丰衣足食的好,而且套件的局限性太大.所以后来就各种搜索,看到前辈写关于wn ...
- C# 字符串处理
1.比较字符串 String 类提供了一系列的方法用于字符串的比较,如CompareTo 和 Equals方法等. ① CompareTo : 如果参数的值与此实例相等,则返回0:如果此实例大于参数 ...
- AngularJs $rootScope.Scope 作用域操作
这里讲的是一些scope的操作,如创建/注销/各种监听及scope间的通信等等. $rootScope.Scope 可以使用$injector通过$rootScope关键字检索的一个根作用域. 可以通 ...
- System.IO.Directory.Delete目录删除
在程序运行的时候,如果直接获取一个目录路径,然后执行删除(包括子目录及文件): System.IO.Directory.Delete(path,true); 或者 System.IO.Director ...
- 墙内无缝更新Android SDK
https://www.caoqq.net/android-sdk-offine-download.html Lucas · 10 个月前 打开Android SDK Manager, 打开设置 2. ...
- NSXMLParser解析本地.xml数据(由于like7xiaoben写的太好了,我从她那里粘贴过来的)
NSXMLParser解析简要说明 .是sax方法解析 .需要创建NSXMLParser实例 (alloc) 并创建解析器 (initWithData:) 为解析器定义委托 (setDelegate: ...