(轉)ASP.NET(C#)FileUpload實現上傳限定類型和大小的檔案到伺服器

來源:互聯網
上載者:User

標籤:.config   head   nat   屬性   public   bsp   .net   一個   ++   

上傳檔案有兩個主要的目的地,一個是伺服器,另一個是資料庫,ASP.NET內建了FileUpload這個上傳控制項,文字框顯示使用者選擇的檔案的全名.

其屬性主要包括:

ContenLength:上傳檔案大小,單位:位元組

FileName:檔案名稱

HasFile:是否選擇了檔案

例子:
測試環境.net 2.0(內有詳細說明)

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" /><br /> 
<br /> 
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="上傳" /> </div> 
</form> 
</body> 
</html>

default.aspx.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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        Boolean fileOk = false;
        string path = Server.MapPath("~/upload/");

//判斷是否已經選取檔案
        if (FileUpload1.HasFile)
        {
            //取得檔案的副檔名,並轉換成小寫
            string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            //限定只能上傳jpg和gif圖片
            string[] allowExtension = { ".jpg", ".gif", ".txt", ".xls" };
            //對上傳的檔案的類型進行一個個匹對
            for (int i = 0; i < allowExtension.Length; i++)
            {
                if (fileExtension == allowExtension[i])
                {
                    fileOk = true;
                    break;
                }
            }
            //
            if (fileOk)
            {
                resultlbl.Text = "要上傳的檔案類型不對!";
            }

            //對上傳檔案的大小進行檢測,限定檔案最大不超過1M
            if (FileUpload1.PostedFile.ContentLength > 1024000)
            {
                fileOk = false;
            }
            //最後的結果
            if (fileOk)
            {
                try
                {
                    FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
                    resultlbl.Text ="上傳成功";
                }
                catch
                {

                    resultlbl.Text = "上傳失敗";
                }
            }
            else
            {
                 resultlbl.Text = "檔案類型或者檔案大小超出1M或者檔案類型不對";

           }

        }
    }
}

(轉)ASP.NET(C#)FileUpload實現上傳限定類型和大小的檔案到伺服器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.