zoukankan      html  css  js  c++  java
  • 个人学习代码保存:例5.利用标准FileUpload单文件上传

    前台代码:Default.aspx
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

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

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title>无标题页</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
            
    <asp:FileUpload ID="FileUpload1" runat="server" />
            
    <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" /></div>
        
    </form>
    </body>
    </html>
    后台代码:Default.aspx.cs
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class _Default : System.Web.UI.Page 
    {
        
    protected void Page_Load(object sender, EventArgs e)
        
    {

        }

        
    protected void Button1_Click(object sender, EventArgs e)
        
    {
            
    if (FileUpload1.HasFile)
            
    {
                
    string FileName = FileUpload1.FileName;
                
    //取得文件类型
                string strFileExtend = FileName.Substring(FileName.LastIndexOf(".")+1);
                
    string[] NoFileExtend ="exe","php","asp"};
                
    bool Flag = true;
                
    for (int i = 0; i < NoFileExtend.Length; i++)
                
    {
                    
    if (strFileExtend.Equals(NoFileExtend[i]))
                    
    {
                        Flag 
    = false;
                    }

                }

                
    if (Flag)
                
    {
                    
    string strFilePath = string.Format("files/{0}.{1}", DateTime.Now.ToString("mmhhddss"), strFileExtend);
                    
    string path = Server.MapPath(strFilePath);
                    FileUpload1.SaveAs(path);
                    Page.ClientScript.RegisterStartupScript(
    this.GetType(), "Startup""<script>alert('文件上传成功');</script>");
                    Response.Write(
    "格式化后的地址为:"+strFilePath+"文件类型为:"+strFileExtend);
                }

                
    else
                
    {

                    Page.ClientScript.RegisterStartupScript(
    this.GetType(), "Startup""<script>alert('" + string.Format("不能上传{0}格式文件", strFileExtend) + "');</script>");
                }

            }

            
    else
            
    {
                Page.ClientScript.RegisterStartupScript(
    this.GetType(),"Startup","<script>alert('文件不能为空');</script>");
            }

        }

    }


  • 相关阅读:
    luogu P1768 天路
    [NOIP2008]双栈排序
    逆元板子集
    [NOIP2015]子串
    [USACO17JAN]Subsequence Reversal
    [NOIP2011] 聪明的质监员
    POJ2987 Firing
    2018.10.20模拟总结
    POJ3469 Dual Core CPU
    真实道路中,如何倒车、侧方停车 哪里是圆心就往哪边大方向 倒车实际是以圆心 画圆后轮进库看左右镜子 别剐蹭
  • 原文地址:https://www.cnblogs.com/wbcms/p/1036658.html
Copyright © 2011-2022 走看看