鱼C论坛

 找回密码
 立即注册
查看: 2864|回复: 0

[学习笔记] C# asp.net实现文件上传

[复制链接]
发表于 2017-6-7 14:18:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
上课了分享C# asp.net实现文件上传代码:

  1. 前端代码:

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

  3. 前端页面代码:

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

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

  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head runat="server">
  8. <script type="text/javascript">   
  9.     var baseText = null;
  10.     function upOpen() {
  11.         var xzOpen = document.getElementById("xzOpen");
  12.         xzOpen.style.top = "200px"; //窗口距离浏览器内容区最上方的偏移值
  13.         xzOpen.style.left = "500px"; //窗口距离浏览器内容区最左边的偏移值
  14.         xzOpen.style.width = "500px"; //窗口的宽度
  15.         xzOpen.style.height = "300px"; //窗口的高度
  16.         if (baseText == null) baseText = xzOpen.innerHTML;
  17.         xzOpen.innerHTML = baseText + "<div id="statusbar"><button onclick="hidePopup(); ">Close window<button></div>"
  18.         var sbar = document.getElementById("statusbar");
  19.         sbar.style.marginTop = (parseInt(100)-20) + "px";
  20.         xzOpen.style.visibility = "visible";
  21.     document.getElementById("xzOpen").click();
  22.     }
  23. </script>
  24. <title>上传文档</title>
  25. <style type="text/css">
  26. *{ margin:0; padding:0;}
  27. .exDiv{}
  28. .boxmain{float:left;margin-right:0px;width:100%;}
  29. .xzOpen{position: absolute; visibility: hidden; overflow: hidden; border:2px solid #CCC; background-color: #FFCBB3; border:2px solid #333; padding:5px; }
  30. .F1{float:left;margin-top:5px;}
  31. .B1{float:right;margin-top:80px;}
  32. .left{position:absolute;left:0; background:#BBFFBB;height:300px;width:20%}
  33. .main{margin-right:200px;background:#79FF79; height:300px;;width:100%;margin-left:auto;}
  34. .up{margin-right:1px;background:#984B4B; height:30px;width:64px;
  35. margin-left:0px;
  36.     }
  37. </style>
  38. </head>
  39. <body>
  40.     <form id="form1" runat="server">
  41.     <div class="exDiv" style="width:100%; height:80px; margin:0 auto; border:solid 1px #999999;background-color:#95CACA">
  42.     <font >上传文件</font>
  43.     </div>
  44.     <div class="xzOpen" id="xzOpen" >
  45.         <div class="F1" id="F1">
  46.             <asp:FileUpload ID="FileUpload1" runat="server" Width="224px"  />
  47.          </div>
  48.          <div class="B1" id="B1">
  49.             <asp:Button ID="Button1" runat="server" Text="提交" onclick="Button1_Click"  style="margin:0 auto" Width="107px" />
  50.          </div>
  51.     </div>
  52.     </form>
  53.     <div class="boxmain">
  54.         <div class="main">main</div>
  55.     </div>
  56.     <div class="left"><input type="file" id="xzFile" style="display:none"/>
  57.          <button type="button" class="up" onclick="upOpen()">选择文件</button>
  58.     </div>
  59. </body>
  60. </html>


  61. 后台C#部分:
  62. using System;
  63. using System.Collections.Generic;
  64. using System.Linq;
  65. using System.Web;
  66. using System.Web.UI;
  67. using System.Web.UI.WebControls;

  68. namespace scientist
  69. {
  70.     public partial class WebForm1 : System.Web.UI.Page
  71.     {

  72.         protected void Page_Load(object sender, EventArgs e)
  73.         {

  74.         }
  75.         protected void Button1_Click(object sender, EventArgs e)
  76.         {
  77.             HttpPostedFile postedFile = this.FileUpload1.PostedFile; //获取到要上传的文件
  78.             String fileName = "";//文件名
  79.             String filePath = "filesNameTest/";//文件保存路径
  80.             fileName = System.IO.Path.GetFileName(postedFile.FileName);//获取文件名称
  81.             if (System.IO.Directory.Exists(Server.MapPath(filePath)) == false)//判断文件夹是否存在
  82.             {
  83.                 System.IO.Directory.CreateDirectory(Server.MapPath(filePath));//如果不存在就创建file文件夹
  84.             }
  85.             if (System.IO.File.Exists(Server.MapPath(filePath+fileName)) == true)//判断同名文件是否存在
  86.             {
  87.                 Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('同名文件已存在')", true);//弹窗提示文件已存在
  88.             }
  89.             else//文件不存在则保存文件
  90.             {
  91.                 if (fileName != "")//判断前端是否有文件传过来
  92.                 {
  93.                     String fileSuffix = System.IO.Path.GetExtension(fileName); //获取上传文件的扩展名
  94.                     postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(filePath) + fileName);//保存文件至根目录下的files文件夹里
  95.                     Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('已经保存成功')", true);//弹窗提示保存成功
  96.                 }
  97.                 else
  98.                 {
  99.                     Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('请选择文件')", true);//弹窗提示未选择文件
  100.                 }
  101.             }
  102.         }
  103.     }
  104. }
复制代码

评分

参与人数 1鱼币 +2 收起 理由
小甲鱼 + 2 支持楼主!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-3 07:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表