关于Extjs MVC模式上传文件的简单方式
Extjs新手研究上传文件的事情估计是件很头痛的问题,毕竟,我就在头痛。最近两天一直在忙文件上传问题,终于小有收获。
用的是Extjs+MVC3.0+EF开发,语言为C#。前台window代码显示列内容
}, {
items: [{
xtype: 'textfield',
fieldLabel: iJobRequirement.iAttachment,
name: 'AttachmentPath',
readOnly: true,
id: '附件',
value: edit ? cfg.record.get("AttachmentPath") : null,
labelWidth: 110,
width: 440,
margin: '0 0 0 0 '
}, {
xtype: 'button',
text: iGeneral.iAdd,
iconCls: 'add',
width: 60,
handler: function (btn) {
var form = btn.up('jobReqWindow').down('form').getForm();
var sId = form.findField('AttachmentPath').id;
Ext.create('FileUploadWindow', {
title: iGeneral.iUpload,
sId: sId
}).show()
}
}]
}, {
这是在jobReqWindow这个窗体中的一行,当我点击attachment旁边的button按钮后会打开一个名字为FileUploadWindow,代码如下:
/**
*description 定义文件上传组件
*author 马海涛
*date 2013-12-30
*/
Ext.define('FileUploadWindow', {
extend: 'Ext.window.Window',
alias: 'widget.fileUploadWindow',
constructor: function (cfg) {
var sId = cfg.sId;
FileUploadWindow.superclass.constructor.call(this, Ext.apply({
width: 450,
//title: cfg.title,
closable: true, //含关闭按钮
resizable: false,
modal: true,
bodyPadding: 10,
frame: true,
items: [{
xtype: 'form',
name: 'winform',
frame: true,
border: 0,
padding: '0',
items: [{
xtype: 'filefield',
name: 'file',
id: 'fileUpload',
margin: '1 0 0 0',
width: 420,
fieldLabel: 'File',
labelWidth: 30,
buttonConfig: {
width: 130,
iconCls: 'upload'
},
readOnly: true,
anchor: '100%',
buttonText: iUploadFile.iFileNote
}]
}],
dockedItems: {
xtype: 'toolbar',
dock: 'bottom',
ui: 'footer',
items: [{
xtype: 'component', flex: 1
}, {
xtype: 'button',
text: iGeneral.iUpload,
width: 55,
handler: function (btn) {
var form = btn.up('fileUploadWindow').down('form').getForm();
form.checkValidity();
if (form.isValid()) {
form.submit({
url: '../QuestionReq/UploadQuestionReq',
waitMsg: iUploadFile.iUploading,
success: function (fp, o) {
Ext.Msg.Show(iUploadFile.iFile + o.result.message + iUploadFile.iUploadSuccessfully, iGeneral.iNote);
btn.up('fileUploadWindow').close();
Ext.getCmp(sId).setValue(o.result.file);
},
failure: function (fp, o) {
Ext.Msg.Show(iUploadFile.iUploadFailed, iGeneral.iNote);
btn.up('fileUploadWindow').close();
}
});
}
}
}, {
xtype: 'button',
text: iGeneral.iCancel,
iconCls: 'delete',
handler: function (btn) { btn.up('fileUploadWindow').close(); }
}]
}
}, cfg));
}
});
这是常见的文件上传写法,用的是表单提交的方式。我用Ajax上传文件没有做成功,网上一些人说文件上传貌似不可以用Ajax,只能用表单。主要代码为
var form = btn.up('fileUploadWindow').down('form').getForm();
form.checkValidity();
if (form.isValid()) {
form.submit({
url: '../QuestionReq/UploadQuestionReq',
waitMsg: iUploadFile.iUploading,
success: function (fp, o) {
Ext.Msg.Show(iUploadFile.iFile + o.result.message + iUploadFile.iUploadSuccessfully, iGeneral.iNote);
btn.up('fileUploadWindow').close();
Ext.getCmp(sId).setValue(o.result.file);
},
failure: function (fp, o) {
Ext.Msg.Show(iUploadFile.iUploadFailed, iGeneral.iNote);
btn.up('fileUploadWindow').close();
}
});
}
}
Ext.getCmp(sId).setValue(o.result.file);将文件名称返回到attachment这个textfield控件上,这样用户体验性更好。
后台处理代码如下:
/// <summary>
/// 上传文件
/// </summary>
/// <returns></returns>
public void UploadQuestionReq()
{
try
{
HttpRequest request = System.Web.HttpContext.Current.Request;
HttpPostedFile myPostedFile = request.Files[];//由于HttpPostedFile才是真正包含文件内容的类,因此再上传文件时将HttpPostedFileBase
string fileName = myPostedFile.FileName; //改为HttpPostedFile
string fileNameExtension = Path.GetExtension(fileName);
if (fileName.LastIndexOf("\\") > -) //为了解决谷歌和IE不兼容的现象,不过好像没有什么作用
{
fileName = fileName.Substring(fileName.LastIndexOf("\\") + );
}
if (myPostedFile.ContentLength > && !string.IsNullOrEmpty(fileName))
{
string savePath = ConfigurationManager.AppSettings["fileUploadPath"];
if (System.IO.Directory.Exists(Server.MapPath(savePath)) == false)
{
System.IO.Directory.CreateDirectory(Server.MapPath(savePath));
}
myPostedFile.SaveAs(Server.MapPath(savePath + Path.GetFileName(fileName)));
Response.Write("{success:true,message:'" + fileName + "',file:'" + Path.GetFileName(fileName) + "'}");
}
else
{
Response.Write("{success:false}");
}
}
catch (Exception ex)
{
throw ex;
}
}
图片为:




关于Extjs MVC模式上传文件的简单方式的更多相关文章
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- SpringMvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- Asp.Net Mvc异步上传文件的方式
今天试了下mvc自带的ajax,发现上传文件时后端action接收不到文件, Request.Files和HttpPostedFileBase都接收不到.....后来搜索了下才知道mvc自带的Ajax ...
- ASP.NET Core MVC如何上传文件及处理大文件上传
用文件模型绑定接口:IFormFile (小文件上传) 当你使用IFormFile接口来上传文件的时候,一定要注意,IFormFile会将一个Http请求中的所有文件都读取到服务器内存后,才会触发AS ...
- FTP主动模式上传文件时返回"ftp: accept: Resource temporarily unavailable"
FTP主动模式上传文件时返回 Passive mode off ftp: accept: Resource temporarily unavailable 这个问题要从ftp的2种模式说起 PORT ...
- HDFS基本命令行操作及上传文件的简单API
一.HDFS基本命令行操作: 1.HDFS集群修改SecondaryNameNode位置到hd09-2 (1)修改hdfs-site.xml <configuration> //配置元数据 ...
- MVC:上传文件时限制文件类型
之前写过一篇:MVC:上传文件 今天补充下一个功能:如何限制上传文件类型 文件类型可以在前段限制,但是太容易被绕过,最好还是在后端处理. 修改upload方法代码: [HttpPost] public ...
- MVC ajax 上传文件
废话不多说,上代码: 用到的js文件: jquery.min.js jquery.easyui.min.js <input id="fileurl" onclick=&quo ...
- spring mvc CommonsMultipartResolver上传文件异常处理
近期已经上线的项目出现了一个异常 严重: Servlet.service() for servlet JeeCmsAdmin threw exception org.apache.commons.fi ...
随机推荐
- nyoj-204
描述国王有一个魔镜,可以把任何接触镜面的东西变成原来的两倍——只是,因为是镜子嘛,增加的那部分是反的. 比如一条项链,我们用AB来表示,不同的字母表示不同颜色的珍珠.如果把B端接触镜面的话,魔镜会把这 ...
- 【iCore3 双核心板】例程十六:USB_HID实验——双向数据传输
实验指导书及代码包下载: http://pan.baidu.com/s/1bojcVoV iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- Java虚拟机内存管理机制
自动内存管理机制 Java虚拟机(JVM)在执行Java程序过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途,以及创建和销毁的时间,有的区域随着虚拟机进程的启动而存在,有的区 ...
- Hibernate - lazy, fetch, inverse, cascade
Inverse是hibernate双向关系中的基本概念.inverse的真正作用就是指定由哪一方来维护之间的关联关系.当一方中指定了"inverse=false"(默认),那么那一 ...
- LeetCode Maximum Size Subarray Sum Equals k
原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...
- 制作图表二、使用图片工厂设置RGB改变图标颜色
亮绿 RGB:76 175 80灰色 RGB:151 151 153
- ArrayBlockingQueue,BlockingQueue分析
BlockingQueue接口定义了一种阻塞的FIFO queue,每一个BlockingQueue都有一个容量,让容量满时往BlockingQueue中添加数据时会造成阻塞,当容量为空时取元素操作会 ...
- (地址)propedit安装说明的地址
proedit http://propedit.sourceforge.jp/eclipse/updates/
- UCOS 解读代码
1.OSInit()函数:建立两个任务,一个是空闲任务,在任何任务没有就绪时运行,一个是统计任务,计算cpu的利用率.初始化 UCOSII 的所有变量和数据结构,2.OSTaskCreate 该函数返 ...
- Leetcode: Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...