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

        }

    }


  • 相关阅读:
    css
    数据库
    手机编码
    火锅开2了
    Linux 与 Virtual PC(VirtualBox)
    切图
    [zz] Loading Master Data without using the PSA in SAP BI 7.0
    Notes: CRM Analytics–BI from a CRM perspective (2)
    Notes: CRM Analytics–BI from a CRM perspective (3)
    Sharing: hints, tcodes, commands that may help when solving support package implementation problems
  • 原文地址:https://www.cnblogs.com/wbcms/p/1036658.html
Copyright © 2020-2023  润新知