• 个人学习代码保存:例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>");
            }

        }

    }


  • 相关阅读:
    asp.net core系列 26 EF模型配置(实体关系)
    asp.net core系列 25 EF模型配置(隐藏属性)
    asp.net core系列 24 EF模型配置(主键,生成值,最大长度,并发标记)
    (办公)TOKEN
    (办公)plug-in org.eclipse.jdt.ui was unable to load class org.eclipse.jdt.internal
    (办公)系统死锁了.
    (办公)MojoExecutionException
    (生活)Photoshop入门(不定时更新)
    (办公)百度api的使用
    (办公)Mysql入门
  • 原文地址:https://www.cnblogs.com/wbcms/p/1036658.html
Copyright © 2020-2023  润新知