189 8069 5689

C#asp.net实现文件上传

前端代码:

使用visual studio开发实现文件上传

前端页面代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="scientist.WebForm1" %>





    
    var baseText = null; 
    function upOpen() {
        var xzOpen = 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 + "Close window
"         var sbar = document.getElementById("statusbar");          sbar.style.marginTop = (parseInt(100)-20) + "px";         xzOpen.style.visibility = "visible";      document.getElementById("xzOpen").click();     }  上传文档   *{ margin:0; padding:0;}  .exDiv{} .boxmain{float:left;margin-right:0px;width:100%;}  .xzOpen{position: absolute; visibility: hidden; overflow: hidden; border:2px solid #CCC; background-color: #FFCBB3; border:2px solid #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;     }                 上传文件     
                                    
                                                         main                       选择文件       后台C#部分: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace scientist {     public partial class WebForm1 : System.Web.UI.Page     {         protected void Page_Load(object sender, EventArgs e)         {         }         protected void Button1_Click(object sender, EventArgs e)         {             HttpPostedFile postedFile = this.FileUpload1.PostedFile; //获取到要上传的文件             String fileName = "";//文件名             String filePath = "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 != "")//判断前端是否有文件传过来                 {                     String fileSuffix = 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);//弹窗提示未选择文件                 }             }         }     } }
标题名称:C#asp.net实现文件上传
文章起源:http://cdxtjz.cn/article/jdopsh.html

联系我们

您好HELLO!
感谢您来到成都网站建设公司,若您有合作意向,请您为我们留言或使用以下方式联系我们, 我们将尽快给你回复,并为您提供真诚的设计服务,谢谢。
  • 电话:028- 86922220 18980695689
  • 商务合作邮箱:631063699@qq.com
  • 合作QQ: 532337155
  • 成都网站设计地址:成都市青羊区锣锅巷31号五金站写字楼6楼

小谭建站工作室

成都小谭网站建设公司拥有多年以上互联网从业经验的团队,始终保持务实的风格,以"帮助客户成功"为已任,专注于提供对客户有价值的服务。 我们已为众企业及上市公司提供专业的网站建设服务。我们不只是一家网站建设的网络公司;我们对营销、技术、管理都有自己独特见解,小谭建站采取“创意+综合+营销”一体化的方式为您提供更专业的服务!

小谭观点

相对传统的成都网站建设公司而言,小谭是互联网中的网站品牌策划,我们精于企业品牌与互联网相结合的整体战略服务。
我们始终认为,网站必须注入企业基因,真正使网站成为企业vi的一部分,让整个网站品牌策划体系变的深入而持久。