- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head>
- <link href='Ext/resources/css/ext-all.css' rel='stylesheet' type='text/css' />
- <script type='text/javascript' src='Ext/adapter/ext/ext-base.js'></script>
- <script type='text/javascript' src='Ext/ext-all.js'></script>
- </head>
- <body>
- <script type="text/javascript">
- Ext.onReady(function(){
- var form = new Ext.form.FormPanel({
- renderTo:'file',
- labelAlign: 'right',
- title: '文件上传',
- labelWidth: 60,
- frame:true,
- url: 'UploadFile.aspx',//fileUploadServlet
- width: 300,
- height:200,
- fileUpload: true,
- items: [
- {
- xtype: 'textfield',
- fieldLabel: '文件名1',
- name: 'file',
- inputType: 'file'//文件类型
- },
- {
- xtype: 'textfield',
- fieldLabel: '文件名2',
- name: 'file',
- inputType: 'file'//文件类型
- },
- {
- xtype: 'textfield',
- fieldLabel: '文件名3',
- name: 'file',
- inputType: 'file'//文件类型
- }
- ],
- buttons: [{
- text: '上传',
- handler: function() {
- form.getForm().submit({
- success: function(form, action){
- Ext.Msg.alert('信息', '文件上传成功!');
- },
- failure: function(){
- Ext.Msg.alert('错误', '文件上传失败');
- }
- });
- }
- }]
- });
- });
- </script>
- <div id="file"></div>
- </body>
- </html>
UploadFileUploadFile.aspx.cs
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.IO; //注意,导入
- namespace WisdomNF
- {
- public partial class UploadFile : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- string jSONString = string.Empty;
- try
- {
- string saveFoler = Server.MapPath("upload_files/");
- string savePath,fileName;
- //遍历File表单元素
- for(int iFile = 0; iFile < Request.Files.Count; iFile++)
- {
- HttpPostedFile postedFile = Request.Files[iFile];
- fileName = Path.GetFileName(postedFile.FileName);
- if (fileName != "")
- {
- string fileType = fileName.Substring(fileName.LastIndexOf("."));
- string newName = Guid.NewGuid().ToString("N")+fileType;
- //string newName = DateTime.Now.ToString("yyMMddHHmmssfff")+iFile.ToString();//精确到毫秒
- savePath = saveFoler + newName;
- //检查是否在服务器上已经存在用户上传的同名文件
- if (File.Exists(savePath))
- {
- File.Delete(savePath);
- }
- postedFile.SaveAs(savePath);
- }
- }
- jSONString = "{success:true,message:'上传完成!'}";
- }//try
- catch (Exception ex)
- {
- jSONString = "{success:false,message:'上传失败,可能因为上传文件过大导致!'}";
- }//catch
- Response.Write(jSONString);
- Response.Flush();
- }
- }
- }