Extjs结合.NET上传文件

 
  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. <link href='Ext/resources/css/ext-all.css' rel='stylesheet' type='text/css' />
  5. <script type='text/javascript' src='Ext/adapter/ext/ext-base.js'></script>
  6. <script type='text/javascript' src='Ext/ext-all.js'></script>
  7. </head>
  8. <body>
  9. <script type="text/javascript">
  10. Ext.onReady(function(){    
  11. var form = new Ext.form.FormPanel({    
  12. renderTo:'file',   
  13. labelAlign: 'right',    
  14. title: '文件上传',    
  15. labelWidth: 60,    
  16. frame:true,   
  17. url: 'UploadFile.aspx',//fileUploadServlet    
  18. width: 300,    
  19. height:200,   
  20. fileUpload: true,   
  21. items: [
  22. {    
  23. xtype: 'textfield',    
  24. fieldLabel: '文件名1',    
  25. name: 'file',
  26. inputType: 'file'//文件类型
  27. },
  28. {    
  29. xtype: 'textfield',
  30. fieldLabel: '文件名2',
  31. name: 'file',    
  32. inputType: 'file'//文件类型 
  33. },
  34. {    
  35. xtype: 'textfield',    
  36. fieldLabel: '文件名3',    
  37. name: 'file',    
  38. inputType: 'file'//文件类型    
  39. }
  40. ],    
  41. buttons: [{    
  42. text: '上传',    
  43. handler: function() {    
  44. form.getForm().submit({    
  45. success: function(form, action){    
  46. Ext.Msg.alert('信息', '文件上传成功!');    
  47. },    
  48. failure: function(){    
  49. Ext.Msg.alert('错误', '文件上传失败');    
  50. }    
  51. });    
  52. }    
  53. }]    
  54. });    
  55. }); 
  56. </script>
  57. <div id="file"></div>
  58. </body>
  59. </html>

UploadFileUploadFile.aspx.cs
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.IO; //注意,导入
  12. namespace WisdomNF
  13. {
  14. public partial class UploadFile : System.Web.UI.Page
  15. {
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. string jSONString = string.Empty;
  19. try
  20. {
  21. string saveFoler = Server.MapPath("upload_files/");
  22. string savePath,fileName;
  23. //遍历File表单元素   
  24. for(int iFile = 0; iFile < Request.Files.Count; iFile++)   
  25. {   
  26. HttpPostedFile postedFile = Request.Files[iFile];
  27. fileName = Path.GetFileName(postedFile.FileName);
  28. if (fileName != "")
  29. {
  30. string fileType = fileName.Substring(fileName.LastIndexOf("."));
  31. string newName = Guid.NewGuid().ToString("N")+fileType;
  32. //string newName = DateTime.Now.ToString("yyMMddHHmmssfff")+iFile.ToString();//精确到毫秒
  33. savePath = saveFoler + newName;
  34. //检查是否在服务器上已经存在用户上传的同名文件
  35. if (File.Exists(savePath))
  36. {
  37. File.Delete(savePath);
  38. }
  39. postedFile.SaveAs(savePath);
  40. }
  41. }
  42. jSONString = "{success:true,message:'上传完成!'}";
  43. }//try
  44. catch (Exception ex)
  45. {
  46. jSONString = "{success:false,message:'上传失败,可能因为上传文件过大导致!'}";
  47. }//catch
  48. Response.Write(jSONString);
  49. Response.Flush();
  50. }
  51. }
  52. }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值