关于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 ...
随机推荐
- php中英字符串截取
<?php @header('Content-type: text/html; charset=UTF-8'); function Ctruncate($str = '', $len = 0, ...
- decode 函数将字符串从某种编码转为 unicode 字符
环境:Ubuntu, Python 2.7 基础知识 这个程序涉及到的知识点有几个,在这里列出来,不详细讲,有疑问的直接百度会有一堆的. 1.urllib2 模块的 request 对像来设置 HTT ...
- Java之方法重载(笔记)
Java是根据参数类型和个数的不同实现重载. 1.当参数类型是基本类型,但不完全匹配. void test(int i) { } void test(float f) { } public stati ...
- 手机上的页面转换page slider
小伙伴们是不是经常在手机上见到“转场"的情况,手机上的页面转换已经不像pc上整体的页面跳转,很多都是利用动画平滑地在页面之间的切换. 那么如何来做页面之间的转换呢?首先要明确的是,所谓的 ...
- 【iCore3 双核心板】例程四:USART通信实验——通过命令控制LED
实验指导书及代码包下载: http://pan.baidu.com/s/1pJxluWF iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- MipMap与三线性过滤
现代计算机图形管线渲染图像的方法是处理这两个问题: 1 3D世界的几何图元如何投影成2D图元,进而对应到屏幕的哪些像素 2 根据已有的信息(光照,法向量,贴图),每个像素点应该怎样设置颜色 根据这两个 ...
- 启动Hive报错
Exception in thread "main" java.lang.RuntimeException: Hive metastore database is not init ...
- Win8.1密钥
Win8.1 在线永久激活密钥一枚! 78BHN-M3KRH-PCP9W-HQJYR-Q9KHD [剩余次数:7K多+] 继续增加 [Key]:HPCJW-VGYW4-CR7W2-JG6Q7-K4Q ...
- Line segment matching
FMII2方法:FMII方法的轻微的修改.有限线段和无限线段(直线)的匹配. 求解方法: SVD分解 Unit Quaternion 协方差矩阵: 通过对C进行SVD分解得到R,根据R求得T. 算法流 ...
- Java基础之扩展GUI——添加状态栏(Sketcher 1 with a status bar)
控制台程序. 为了显示各个应用程序参数的状态,并且将各个参数显示在各自的面板中,在应用程序窗口的底部添加状态栏是常见且非常方便的方式. 定义状态栏时没有Swing类可用,所以必须自己建立StatusB ...