asp.net上传文件

来源:互联网 发布:淘宝买东西银行卡付款 编辑:程序博客网 时间:2024/06/10 08:59

//运行环境Microsoft Visual Studio 2005

//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>
        
&nbsp;<asp:FileUpload ID="FileUpload1" runat="server" />
        
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" /><br />
        文件路径:
<asp:Label ID="Label1" runat="server" Height="21px" Width="709px"></asp:Label><br />
        
<asp:Label ID="Label2" runat="server" Width="136px"></asp:Label>&nbsp;<br />
        
<asp:Image ID="Image1" runat="server" Height="136px" Width="230px" />
        
&nbsp;&nbsp;&nbsp;<br />
        
&nbsp;
        
</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)
    
{
        Image1.Visible 
= false;
    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        
        
if (FileUpload1.PostedFile.FileName!=null
        
{
            
//文件路径    
            string name1 = FileUpload1.PostedFile.FileName;

           
// Image1.ImageUrl=name1;
            
//文件名称    
            string name2 = System.IO.Path.GetFileName(name1);
            
            
//文件大小
            int  filelongth = FileUpload1.PostedFile.ContentLength;
               
            
int tt = name1.LastIndexOf(".");
            
//文件后缀    
            string filetype = name1.Substring(tt);
            
            
//文件后缀转换成小写字母
            filetype= filetype.ToLower();
            
            
//检查文件的格式
            if (filetype != ".gif" && filetype != ".jpg" && filetype != ".jpeg" && filetype != ".bmp" && filetype != ".png")
                
{
                Response.Write(
"文件格式不正确!");
                    Response.End();
                }

                
else 
                
{
                    
try
                    
{
                      
                        FileUpload1.SaveAs(Server.MapPath(
"picture"+ "/" + FileUpload1.FileName);
                        Image1.ImageUrl 
= Server.MapPath("picture"+ "/" + FileUpload1.FileName;
                        Image1.Visible 
= true;
                        Label1.Text 
= name1;
                        Label2.Text 
= System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);     
                    }

                    
catch (Exception ex)
                    

                    }

                
                }

        
        }

           
        
else
        
{ Label1.Text = "You have not specified a file."; } 
        
    }

}


原创粉丝点击