FileUpload控件

来源:互联网 发布:人工智能电影 编辑:程序博客网 时间:2024/05/22 11:58
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 FileUpload : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        
bool fileOK = false;
        
string path = Server.MapPath("~/temp/");
        
if (FileUpload1.HasFile)
        
{
            String fileExtension 
= System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] allowedExtension 
=".gif"".png"".bmp"".jpg" };
            
for (int i = 0; i < allowedExtension.Length; i++)
            
{
                
if (fileExtension == allowedExtension[i])
                
{ fileOK = true; }
            }

        }

        
if (fileOK)
        
{
            
try
            
{
                FileUpload1.SaveAs(path 
+ FileUpload1.FileName);
                Label1.Text 
= "文件上传成功!";
            }

            
catch (Exception ex)
            
{ Label1.Text = "文件上传不成功!"; }
        }

        
else
        
{ Label1.Text = "只能上传图片文件!"; }
    }

}

原创粉丝点击