文件上传代码

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUpload.aspx.cs" Inherits="FileUpload" %>

<!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>

<script language="javascript">

function CheckImg(FileUpload)

{

var mime=FileUpload.value;

    mime=mime.toLowerCase().substr(mime.lastIndexOf("."));

if(mime!=".jpg")

{

FileUpload.value="";

alert("仅支持JPG格式");

}

}

</script>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:FileUpload  onchange="CheckImg(this)" ID="fulFile" runat="server" Style="z-index: 100; left: 30px; position: absolute;

            top: 61px" />

        <asp:Label ID="lblFile" runat="server" Height="63px" Style="z-index: 101; left: 26px;

            position: absolute; top: 128px" Width="268px"></asp:Label>

        <asp:Button ID="btnFile" runat="server" OnClick="btnFile_Click" Style="z-index: 102;

            left: 30px; position: absolute; top: 91px" Text="上传" Width="105px" />

       </div>

    </form>

</body>

</html>


CS 

 

 

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 btnFile_Click(object sender, EventArgs e)

    {

        if (fulFile.HasFile)

        {

            try

            {

                fulFile.SaveAs("C:\\" + fulFile.FileName);

                lblFile.Text = "上传的文件名为:" + fulFile.FileName + "<br>" +

                    "文件大小:" + fulFile.PostedFile.ContentLength + "kb<br>" +

                    "文件类型:" + fulFile.PostedFile.ContentType;

            }

            catch (Exception ex)

            {

                lblFile.Text = ex.Message;

            }

        }

        else

        {

            lblFile.Text = "您没有选择任何文件!";

        }

    }

}

 

 

 

 

 

 

 

 

posted @ 2008-11-05 23:38  Edward Xie  阅读(323)  评论(0编辑  收藏  举报