ASP.NET上载简单案例_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > ASP.NET上载简单案例

ASP.NET上载简单案例

 2013/7/13 23:15:17  庭上杨柳  博客园  我要评论(0)
  • 摘要:1:<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="login.aspx.cs"Inherits="link_mysql.login"%>2:3:<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4:5:<
  • 标签:.net ASP.NET net
class="alt">   1:  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="link_mysql.login" %>
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:   
   5:  <html xmlns="http://www.w3.org/1999/xhtml">
   6:  <head runat="server">
   7:      <title></title>
   8:      <script type="text/javascript">
   9:          function checktype() {
  10:              var allow = [".doc", ".docx"];
  11:              var uploadfile = document.getElementById("FileUpload1").value;
  12:              var ext_p = uploadfile.lastIndexOf(".");
  13:              var ext = uploadfile.substring(ext_p).toLowerCase();
  14:              for (var i = 0; i < allow.length; i++) {
  15:                  if (allow[i] == ext) {
  16:                      return true;
  17:                  }
  18:              }
  19:              alert("file not valid."+ext);
  20:              return false;
  21:          }
  22:      </script>
  23:  </head>
  24:  <body>
  25:      <form id="form1" runat="server">
  26:      <asp:FileUpload ID="FileUpload1" runat="server" />&nbsp;
  27:      <asp:Button ID="Button1" runat="server" Text="Button" OnClick="btn_uploadFile"  OnClientClick="return checktype()"/>&nbsp;
  28:      <asp:Label ID="Label1" runat="server" Text="未上传"></asp:Label>
  29:      </form>  
  30:      <div runat="server" id="dlfiles">
  31:      文件下载:<br />
  32:      </div> 
  33:  </body>
  34:  </html>

—>cs页

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:  using System.Web.UI;
   6:  using System.Web.UI.WebControls;
   7:   
   8:  namespace link_mysql
   9:  {
  10:      public partial class login : System.Web.UI.Page
  11:      {
  12:          protected void Page_Load(object sender, EventArgs e)
  13:          {
  14:   
  15:          }
  16:          public void btn_uploadFile(object sender, EventArgs e)
  17:          {
  18:              if (FileUpload1.HasFile)
  19:              {
  20:                  if (FileUpload1.PostedFile.ContentLength >= 10000000)
  21:                  {
  22:                      Label1.Text = "文件太大";
  23:                  }
  24:                  else
  25:                  {
  26:                      FileUpload1.SaveAs(Server.MapPath("~/Files/")+FileUpload1.FileName);
  27:                      Label1.Text = "Success.";
  28:                      dlfiles.InnerHtml =dlfiles.InnerHtml+
  29:                          "<a href='" + @"Files/"+FileUpload1.FileName+"'>"+FileUpload1.FileName+"</a><br/>";
  30:                  }
  31:              }
  32:   
  33:          }
  34:      }
  35:  }
发表评论
用户名: 匿名