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模式上传文件的简单方式的更多相关文章

  1. spring mvc(注解)上传文件的简单例子

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  2. SpringMvc(注解)上传文件的简单例子

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  3. Asp.Net Mvc异步上传文件的方式

    今天试了下mvc自带的ajax,发现上传文件时后端action接收不到文件, Request.Files和HttpPostedFileBase都接收不到.....后来搜索了下才知道mvc自带的Ajax ...

  4. ASP.NET Core MVC如何上传文件及处理大文件上传

    用文件模型绑定接口:IFormFile (小文件上传) 当你使用IFormFile接口来上传文件的时候,一定要注意,IFormFile会将一个Http请求中的所有文件都读取到服务器内存后,才会触发AS ...

  5. FTP主动模式上传文件时返回"ftp: accept: Resource temporarily unavailable"

    FTP主动模式上传文件时返回 Passive mode off ftp: accept: Resource temporarily unavailable 这个问题要从ftp的2种模式说起 PORT ...

  6. HDFS基本命令行操作及上传文件的简单API

    一.HDFS基本命令行操作: 1.HDFS集群修改SecondaryNameNode位置到hd09-2 (1)修改hdfs-site.xml <configuration> //配置元数据 ...

  7. MVC:上传文件时限制文件类型

    之前写过一篇:MVC:上传文件 今天补充下一个功能:如何限制上传文件类型 文件类型可以在前段限制,但是太容易被绕过,最好还是在后端处理. 修改upload方法代码: [HttpPost] public ...

  8. MVC ajax 上传文件

    废话不多说,上代码: 用到的js文件: jquery.min.js jquery.easyui.min.js <input id="fileurl" onclick=&quo ...

  9. spring mvc CommonsMultipartResolver上传文件异常处理

    近期已经上线的项目出现了一个异常 严重: Servlet.service() for servlet JeeCmsAdmin threw exception org.apache.commons.fi ...

随机推荐

  1. java环境搭建系列:JDK环境变量详细配置

    学习java语言,编写java程序,运行java程序,都离不开Java环境的支持,最重要的就是安装JDK,JDK给我提供了java程序的开发环境和运行环境.为了让java程序放在电脑的任意位置都可以执 ...

  2. php读取文件里面的数组做为配置文件

    可能大家也都见过很多开源的产品,大多它们的配置文件都存放在一个单独的文件中,而这个文件里只存放了一个数组,其实这里运用了一个PHP的小技巧,就是可以将文件包含进来,并且赋值给一个变量,这个变量就具有了 ...

  3. 【java基础学习】泛型

    泛型 1. 泛型类(声明的泛型类型静态方法不能使用) class Tools<T>{ private T t; public void set(T t){ this.t = t; } pu ...

  4. Windows下mock环境搭建-加速项目Api开发

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 公司进行技术部拆分,以项目制作为新的开发模式,前端+移动端+后端,于是加速Api开发变得很有必要,准 ...

  5. An AVPlayerItem cannot be associated with more than one instance of AVPlayer错误

    An AVPlayerItem cannot be associated with more than one instance of AVPlayer An AVPlayerItem cannot ...

  6. bat批处理完成jdk tomcat的安装

    在完成一个web应用项目后,领导要求做一个配置用的批处理文件,能够自动完成jdk的安装,tomcat的安装,web应用的部署,环境变量的注册,tomcat服务的安装和自动启动 参考了网上很多的类似的批 ...

  7. angularJs内置指令63个

  8. [转]C++11 多线程

    转载自:http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438288.html C++11开始支持多线程编程,之前多线程编程都需要系统的支持, ...

  9. mysqladmin note

    hr,fresh meat!! --------------------------------------------------- 15 Practical Usages of Mysqladmi ...

  10. CAS无锁算法与ConcurrentLinkedQueue

    CAS:Compare and Swap 比较并交换 java.util.concurrent包完全建立在CAS之上的,没有CAS就没有并发包.并发包借助了CAS无锁算法实现了区别于synchroni ...