C# asp.net实现文件上传
前端代码:使用visualstudio开发实现文件上传前端页面代码:<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="WebForm1.aspx.cs"Inherits="scientist.WebForm1"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><scripttype="text/javascript">varbaseText=null;functionupOpen(){varxzOpen=document.getElementById("xzOpen");xzOpen.style.top="200px";//窗口距离浏览器内容区最上方的偏移值xzOpen.style.left="500px";//窗口距离浏览器内容区最左边的偏移值xzOpen.style.width="500px";//窗口的宽度xzOpen.style.height="300px";//窗口的高度if(baseText==null)baseText=xzOpen.innerHTML;xzOpen.innerHTML=baseText+"<divid=\"statusbar\"><buttononclick=\"hidePopup();\">Closewindow<button></div>"varsbar=document.getElementById("statusbar");sbar.style.marginTop=(parseInt(100)-20)+"px";xzOpen.style.visibility="visible";document.getElementById("xzOpen").click();}</script><title>上传文档</title><styletype="text/css">*{margin:0;padding:0;}.exDiv{}.boxmain{float:left;margin-right:0px;width:100%;}.xzOpen{position:absolute;visibility:hidden;overflow:hidden;border:2pxsolid#CCC;background-color:#FFCBB3;border:2pxsolid#333;padding:5px;}.F1{float:left;margin-top:5px;}.B1{float:right;margin-top:80px;}.left{position:absolute;left:0;background:#BBFFBB;height:300px;width:20%}.main{margin-right:200px;background:#79FF79;height:300px;;width:100%;margin-left:auto;}.up{margin-right:1px;background:#984B4B;height:30px;width:64px;margin-left:0px;}</style></head><body><formid="form1"runat="server"><divclass="exDiv"><font>上传文件</font></div><divclass="xzOpen"id="xzOpen"><divclass="F1"id="F1"><asp:FileUploadID="FileUpload1"runat="server"Width="224px"/></div><divclass="B1"id="B1"><asp:ButtonID="Button1"runat="server"Text="提交"onclick="Button1_Click"Width="107px"/></div></div></form><divclass="boxmain"><divclass="main">main</div></div><divclass="left"><inputtype="file"id="xzFile"/><buttontype="button"class="up"onclick="upOpen()">选择文件</button></div></body></html>后台C#部分:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespacescientist{publicpartialclassWebForm1:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidButton1_Click(objectsender,EventArgse){HttpPostedFilepostedFile=this.FileUpload1.PostedFile;//获取到要上传的文件StringfileName="";//文件名StringfilePath="filesNameTest/";//文件保存路径fileName=System.IO.Path.GetFileName(postedFile.FileName);//获取文件名称if(System.IO.Directory.Exists(Server.MapPath(filePath))==false)//判断文件夹是否存在{System.IO.Directory.CreateDirectory(Server.MapPath(filePath));//如果不存在就创建file文件夹}if(System.IO.File.Exists(Server.MapPath(filePath+fileName))==true)//判断同名文件是否存在{Page.ClientScript.RegisterStartupScript(this.GetType(),"message","alert('同名文件已存在')",true);//弹窗提示文件已存在}else//文件不存在则保存文件{if(fileName!="")//判断前端是否有文件传过来{StringfileSuffix=System.IO.Path.GetExtension(fileName);//获取上传文件的扩展名postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(filePath)+fileName);//保存文件至根目录下的files文件夹里Page.ClientScript.RegisterStartupScript(this.GetType(),"message","alert('已经保存成功')",true);//弹窗提示保存成功}else{Page.ClientScript.RegisterStartupScript(this.GetType(),"message","alert('请选择文件')",true);//弹窗提示未选择文件}}}}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。