C#上傳檔案的一個實現

來源:互聯網
上載者:User

using System;
using System.Data;
using System.Data.SqlClient;
using CA.Components;        //全部在組件名稱空間下

namespace CA.Components
{
    /// <summary>
    /// General 的摘要說明。
    /// 發布日期:2002-8-8 原作者:雷神
    /// 此程式屬模式小組 HTTP://WWW.AI361.COM/PROJECT/
    /// 在sql2000,WIN2000s+.net+iis5中測試通過
    /// </summary>
    public class General
    {
        private string FilePath; //檔案路徑

        //定義一個枚舉用來存放檔案的資訊       
        public enum File
        {
            FILE_SIZE ,        //大小
            FILE_POSTNAME,    //
            FILE_SYSNAME ,
            FILE_ORGINNAME,
            FILE_PATH
        };
        //建構函式
        public general()
        {
            //在WEB.CONFIG中設定AppSettings["filepath"],用於存放檔案的路徑。
            FilePath = System.Configuration.ConfigurationSettings.AppSettings["filepath"];
        }

        /// <summary>
        /// 上傳檔案通用函數,此方法為靜態,系統任何時候均可調用。
        /// </summary>
        /// <param name="file">參數為頁面的FILE控制項對象</param>
        /// <returns></returns>
        public static string[] UploadFile(HtmlInputFile file)
        {
            string[] arr = new String[5];
            //通過系統時間組建檔案名,此功能可以封閉掉,不過中文長檔名支援的不好。
            string FileName = DateTime.Now.ToString().Replace(" ","").Replace(":","").Replace("-","");
            string FileOrginName = file.PostedFile.FileName.Substring(file.PostedFile.FileName.LastIndexOf("\\")+1);
            if(file.PostedFile.ContentLength<=0)
                return null;
            string  postFileName;
            string path = new general().FilePath+"\\";
            try
            {
                int pos = file.PostedFile.FileName.LastIndexOf(".")+1;
                postFileName = file.PostedFile.FileName.Substring(pos,file.PostedFile.FileName.Length-pos);
                file.PostedFile.SaveAs(path+FileName+"."+postFileName);
            }
            catch(Exception exec)
            {
                throw(exec);
            }
            double unit = 1024;
            double size =  Math.Round(file.PostedFile.ContentLength/unit,2);
            arr[(int)File.FILE_SIZE] = size.ToString();//檔案大小
            arr[(int)File.FILE_POSTNAME] = postFileName;//檔案類型(檔案尾碼名)
            arr[(int)File.FILE_SYSNAME] = FileName;//檔案系統名
            arr[(int)File.FILE_ORGINNAME] = FileOrginName;//檔案原來的名字
            arr[(int)File.FILE_PATH]=path+FileName+"."+postFileName;//檔案路徑
            return arr;
            //throw(new Exception(HtmlUtility.HtmlEncode(IDNO.PostedFile.FileName)));
        }
      }
   }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.