- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <head>
- <linkhref='Ext/resources/css/ext-all.css'rel='stylesheet'type='text/css'/>
- <scripttype='text/javascript'src='Ext/adapter/ext/ext-base.js'></script>
- <scripttype='text/javascript'src='Ext/ext-all.js'></script>
- </head>
- <body>
- <scripttype="text/javascript">
- Ext.onReady(function(){
- varform=newExt.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>
- <divid="file"></div>
- </body>
- </html>
UploadFileUploadFile.aspx.cs
usingSystem; usingSystem.Data; usingSystem.Configuration; usingSystem.Collections; usingSystem.Web; usingSystem.Web.Security; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; usingSystem.Web.UI.WebControls.WebParts; usingSystem.Web.UI.HtmlControls; usingSystem.IO;//注意,导入 namespaceWisdomNF { publicpartialclassUploadFile:System.Web.UI.Page { protectedvoidPage_Load(objectsender,EventArgse) { stringjSONString=string.Empty; try { stringsaveFoler=Server.MapPath("upload_files/"); stringsavePath,fileName; //遍历File表单元素 for(intiFile=0;iFile<Request.Files.Count;iFile++) { HttpPostedFilepostedFile=Request.Files[iFile]; fileName=Path.GetFileName(postedFile.FileName); if(fileName!="") { stringfileType=fileName.Substring(fileName.LastIndexOf(".")); stringnewName=Guid.NewGuid().ToString("N")+fileType; //stringnewName=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(Exceptionex) { jSONString="{success:false,message:'上传失败,可能因为上传文件过大导致!'}"; }//catch Response.Write(jSONString); Response.Flush(); } } }