C#编程之 asp.net实现文件上传
白羽 2018-07-10 来源 :网络 阅读 1433 评论 0

摘要:本文将带你了解C#编程之 asp.net实现文件上传,希望本文对大家学C#/.Net有所帮助。





前端代码:

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

前端页面代码:

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

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="//www.w3.org/1999/xhtml">

<head runat="server">

<script type="text/javascript">    

    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 + "<div id=\"statusbar\"><button onclick=\"hidePopup(); \">Close window<button></div>"

        var sbar = document.getElementById("statusbar");

        sbar.style.marginTop = (parseInt(100)-20) + "px";

        xzOpen.style.visibility = "visible";

    document.getElementById("xzOpen").click();

    }

</script>

 <title>上传文档</title>

<style type="text/css">

*{ 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;

    }

</style>

</head>

<body>

    <form id="form1" runat="server">

    <div class="exDiv" style="width:100%; height:80px; margin:0 auto; border:solid 1px #999999;background-color:#95CACA">

    <font >上传文件</font>

    </div>

    <div class="xzOpen" id="xzOpen" >

        <div class="F1" id="F1">

            <asp:FileUpload ID="FileUpload1" runat="server" Width="224px"  />

         </div>

         <div class="B1" id="B1">

            <asp:Button ID="Button1" runat="server" Text="提交" onclick="Button1_Click"  style="margin:0 auto" Width="107px" />

         </div>

    </div>

    </form>

    <div class="boxmain">

        <div class="main">main</div>

    </div>

    <div class="left"><input type="file" id="xzFile" style="display:none"/>

         <button type="button" class="up" onclick="upOpen()">选择文件</button>

    </div>

</body>

</html>

 

 

后台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#.NET的相关知识,希望对C#.NET有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言C#.NET频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程