extjs 上传文件 fileUpload

来源:互联网 发布:淘宝一元起拍 编辑:程序博客网 时间:2024/06/02 06:10

上传文件要是用asp.net的控件上传其实是很简单的问题,要是为了用户体验的提高很多的web开发人员都会采取很炫的ajax上传。

或是用flash想用的控件上传。今天我们看看extjs中如何很炫的上传文件或图片。

查了查资料发现extjs上传文件需要一个js文件FileUploadField.js,在extjs类库文件里可以找到。

FileUploadField.js代码如下:

[javascript] view plaincopyprint?
  1. /*!
  2. * Ext JS Library 3.2.0
  3. * Copyright(c) 2006-2010 Ext JS, Inc.
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. Ext.ns('Ext.ux.form');
  8. /**
  9. * @class Ext.ux.form.FileUploadField
  10. * @extends Ext.form.TextField
  11. * Creates a file upload field.
  12. * @xtype fileuploadfield
  13. */
  14. Ext.ux.form.FileUploadField = Ext.extend(Ext.form.TextField, {
  15. /**
  16. * @cfg {String} buttonText The button text to display on the upload button (defaults to
  17. * 'Browse...'). Note that if you supply a value for {@link #buttonCfg}, the buttonCfg.text
  18. * value will be used instead if available.
  19. */
  20. buttonText: 'Browse...',
  21. /**
  22. * @cfg {Boolean} buttonOnly True to display the file upload field as a button with no visible
  23. * text field (defaults to false). If true, all inherited TextField members will still be available.
  24. */
  25. buttonOnly: false,
  26. /**
  27. * @cfg {Number} buttonOffset The number of pixels of space reserved between the button and the text field
  28. * (defaults to 3). Note that this only applies if {@link #buttonOnly} = false.
  29. */
  30. buttonOffset: 3,
  31. /**
  32. * @cfg {Object} buttonCfg A standard {@link Ext.Button} config object.
  33. */
  34. // private
  35. readOnly: true,
  36. /**
  37. * @hide
  38. * @method autoSize
  39. */
  40. autoSize: Ext.emptyFn,
  41. // private
  42. initComponent: function(){
  43. Ext.ux.form.FileUploadField.superclass.initComponent.call(this);
  44. this.addEvents(
  45. /**
  46. * @event fileselected
  47. * Fires when the underlying file input field's value has changed from the user
  48. * selecting a new file from the system file selection dialog.
  49. * @param {Ext.ux.form.FileUploadField} this
  50. * @param {String} value The file value returned by the underlying file input field
  51. */
  52. 'fileselected'
  53. );
  54. },
  55. // private
  56. onRender : function(ct, position){
  57. Ext.ux.form.FileUploadField.superclass.onRender.call(this, ct, position);
  58. this.wrap = this.el.wrap({cls:'x-form-field-wrap x-form-file-wrap'});
  59. this.el.addClass('x-form-file-text');
  60. this.el.dom.removeAttribute('name');
  61. this.createFileInput();
  62. var btnCfg = Ext.applyIf(this.buttonCfg || {}, {
  63. text: this.buttonText
  64. });
  65. this.button = new Ext.Button(Ext.apply(btnCfg, {
  66. renderTo: this.wrap,
  67. cls: 'x-form-file-btn' + (btnCfg.iconCls ?' x-btn-icon' : '')
  68. }));
  69. if(this.buttonOnly){
  70. this.el.hide();
  71. this.wrap.setWidth(this.button.getEl().getWidth());
  72. }
  73. this.bindListeners();
  74. this.resizeEl = this.positionEl =this.wrap;
  75. },
  76. bindListeners: function(){
  77. this.fileInput.on({
  78. scope: this,
  79. mouseenter: function() {
  80. this.button.addClass(['x-btn-over','x-btn-focus'])
  81. },
  82. mouseleave: function(){
  83. this.button.removeClass(['x-btn-over','x-btn-focus','x-btn-click'])
  84. },
  85. mousedown: function(){
  86. this.button.addClass('x-btn-click')
  87. },
  88. mouseup: function(){
  89. this.button.removeClass(['x-btn-over','x-btn-focus','x-btn-click'])
  90. },
  91. change: function(){
  92. var v = this.fileInput.dom.value;
  93. this.setValue(v);
  94. this.fireEvent('fileselected',this, v);
  95. }
  96. });
  97. },
  98. createFileInput : function() {
  99. this.fileInput = this.wrap.createChild({
  100. id: this.getFileInputId(),
  101. name: this.name||this.getId(),
  102. cls: 'x-form-file',
  103. tag: 'input',
  104. type: 'file',
  105. size: 1
  106. });
  107. },
  108. reset : function(){
  109. this.fileInput.remove();
  110. this.createFileInput();
  111. this.bindListeners();
  112. Ext.ux.form.FileUploadField.superclass.reset.call(this);
  113. },
  114. // private
  115. getFileInputId: function(){
  116. return this.id + '-file';
  117. },
  118. // private
  119. onResize : function(w, h){
  120. Ext.ux.form.FileUploadField.superclass.onResize.call(this, w, h);
  121. this.wrap.setWidth(w);
  122. if(!this.buttonOnly){
  123. var w = this.wrap.getWidth() -this.button.getEl().getWidth() - this.buttonOffset;
  124. this.el.setWidth(w);
  125. }
  126. },
  127. // private
  128. onDestroy: function(){
  129. Ext.ux.form.FileUploadField.superclass.onDestroy.call(this);
  130. Ext.destroy(this.fileInput, this.button, this.wrap);
  131. },
  132. onDisable: function(){
  133. Ext.ux.form.FileUploadField.superclass.onDisable.call(this);
  134. this.doDisable(true);
  135. },
  136. onEnable: function(){
  137. Ext.ux.form.FileUploadField.superclass.onEnable.call(this);
  138. this.doDisable(false);
  139. },
  140. // private
  141. doDisable: function(disabled){
  142. this.fileInput.dom.disabled = disabled;
  143. this.button.setDisabled(disabled);
  144. },
  145. // private
  146. preFocus : Ext.emptyFn,
  147. // private
  148. alignErrorIcon : function(){
  149. this.errorIcon.alignTo(this.wrap,'tl-tr', [2, 0]);
  150. }
  151. });
  152. Ext.reg('fileuploadfield', Ext.ux.form.FileUploadField);
  153. // backwards compat
  154. Ext.form.FileUploadField = Ext.ux.form.FileUploadField;

fileupload.html,显示上传的效果代码如下:

[xhtml] view plaincopyprint?
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title></title>
  5. <link rel="stylesheet" type="text/css" href="ext/resources/css/ext-all.css" mce_href="ext/resources/css/ext-all.css"/>
  6. <script type="text/javascript" src="ext/adapter/ext/ext-base.js" mce_src="ext/adapter/ext/ext-base.js"></script>
  7. <script src="ext/ext-all-debug.js" mce_src="ext/ext-all-debug.js" type="text/javascript"></script>
  8. <script src="js/FileUploadField.js" mce_src="js/FileUploadField.js" type="text/javascript"></script>
  9. <style type=text/css> .upload-icon {
  10. background: url('images/image_add.png') no-repeat 0 0 !important;
  11. }
  12. .x-form-file-wrap {
  13. position: relative;
  14. height: 22px;
  15. }
  16. .x-form-file-wrap .x-form-file {
  17. position: absolute;
  18. right: 0;
  19. -moz-opacity: 0;
  20. filter:alpha(opacity: 0);
  21. opacity: 0;
  22. z-index: 2;
  23. height: 22px;
  24. }
  25. .x-form-file-wrap .x-form-file-btn {
  26. position: absolute;
  27. right: 0;
  28. z-index: 1;
  29. }
  30. .x-form-file-wrap .x-form-file-text {
  31. position: absolute;
  32. left: 0;
  33. z-index: 3;
  34. color: #777;
  35. }
  36. </style>
  37. <script type="text/javascript"><!--
  38. Ext.onReady(function() {
  39. Ext.QuickTips.init();
  40. var msg = function(title, msg) {
  41. Ext.Msg.show({
  42. title: title,
  43. msg: msg,
  44. minWidth: 200,
  45. modal: true,
  46. icon: Ext.Msg.INFO,
  47. buttons: Ext.Msg.OK
  48. });
  49. };
  50. var fp = new Ext.FormPanel({
  51. renderTo: 'fi-form',
  52. fileUpload: true,
  53. width: 500,
  54. frame: true,
  55. title: '图片上传操作',
  56. autoHeight: true,
  57. bodyStyle: 'padding: 10px 10px 0 10px;',
  58. labelWidth: 50,
  59. defaults: {
  60. anchor: '95%',
  61. allowBlank: false,
  62. msgTarget: 'side'
  63. },
  64. items: [{
  65. xtype: 'textfield',
  66. fieldLabel: 'Name',
  67. name:"txtname"
  68. }, {
  69. xtype: 'fileuploadfield',
  70. id: 'form-file',
  71. emptyText: 'Select an image',
  72. fieldLabel: 'Photo',
  73. name: 'photo-path',
  74. buttonText: '',
  75. buttonCfg: {
  76. iconCls: 'upload-icon'
  77. }
  78. }],
  79. buttons: [{
  80. text: 'Save',
  81. handler: function() {
  82. if (fp.getForm().isValid()) {
  83. fp.getForm().submit({
  84. url: 'Default.aspx',//后台处理的页面
  85. waitMsg: 'Uploading your photo...',
  86. success: function(fp, o) {
  87. msg('Success', 'Processed file "' + o.result.files + '" on the server');
  88. }
  89. });
  90. }
  91. }
  92. }, {
  93. text: 'Reset',
  94. handler: function() {
  95. fp.getForm().reset();
  96. }
  97. }]
  98. });
  99. });
  100. // --></script>
  101. </head>
  102. <body>
  103. <div id="fi-form"></div>
  104. </body>
  105. </html>

以上的代码都是显示上传的内容,而上传的关键在于后台是如何操作。

可以新建一个Default.aspx页面来进行上传的处理。在上传的提交时都是post进行提交的,所以关键是在后台要获取上传的对象。

在Page_Load中加入代码:

[c-sharp] view plaincopyprint?
  1. string newname = Request["txtname"];//获取重命名
  2. HttpPostedFile postedFile = Request.Files["photo-path"];//获取上传信息对象
  3. string filename = postedFile.FileName;//获取上传的文件路径
  4. string tempPath = System.Configuration.ConfigurationManager.AppSettings["NewsFolderPath"];//获取保存文件夹路径,我是设置在webconfig中了。
  5. string savepath = Server.MapPath(tempPath);//获取保存路径
  6. string sExtension = filename.Substring(filename.LastIndexOf('.'));//获取拓展名
  7. if (!Directory.Exists(savepath))
  8. Directory.CreateDirectory(savepath);
  9. string sNewFileName = DateTime.Now.ToString("yyyyMMddhhmmsfff");
  10. postedFile.SaveAs(savepath + @"/" + sNewFileName + sExtension);//保存
  11. Response.Write("{success:true, files:'文件路径:" + filename +" 重命名" + newname + "'}");

演示图如下:

上传中:

上传ok:


原文链接:http://blog.csdn.net/lzy_1515/article/details/5734500


0 0
原创粉丝点击