FileUpload Control

FileUpload Control 은 쉽게 파일을 업로드 할두 있는게 하는 컨트롤 입니다.
아래 (.gif) 와 (.jpg) 형식의 이미지를 업로드 시키는 DEMO 입니다. 저장 경로는 D 드라이브 입니다.
ASP.NET 웹 사이트를 만들고 "FileUpload"라는 폴더를 만듭니다. 이 폴더 안에 "FileUploadControlTest02"라는 WebForm을 추가 합니다.
Result:

==================================
FileUploadControlTest02.aspx
=======================

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        string savePath = @"D:\";

        if (FileUpload1.HasFile)
        {
            string fileName = FileUpload1.FileName;
                       
            string extension = System.IO.Path.GetExtension(fileName);

            if (extension == ".gif" | extension == ".jpg")
            {
                int contentLength = FileUpload1.PostedFile.ContentLength;
                string contentType = FileUpload1.PostedFile.ContentType;
                Response.Write("FileName is: " + fileName + "<br/>" + "FileContentLength is: " + contentLength + "<br/>" + "ContentType is: "
                    + contentType + "<br/>");
               
                savePath += fileName;
                FileUpload1.SaveAs(savePath);
                Label1.ForeColor = System.Drawing.Color.Blue;
                Label1.Text = "Your file was uploaded.";
            }
            else
            {
                Label1.ForeColor = System.Drawing.Color.Red;
                Label1.Text = "Extension of file was not correct.";
            }
        }
        else
        {
            Label1.ForeColor = System.Drawing.Color.Red;          
            Label1.Text = "Please upload your file.";
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>FileUploadControlTest02</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" /><br />
        <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
    </div>
    </form>
</body>
</html>


 

posted @ 2009-02-08 23:24  OOK  阅读(161)  评论(0编辑  收藏  举报