科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道[冷枫].NET上传控件源码[HtmlInputFileControl]

[冷枫].NET上传控件源码[HtmlInputFileControl]

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

.NET上传控件源码[HtmlInputFileControl],可以直接用的。

作者:冷枫 来源:CSDN 2007年9月23日

关键字: 冷枫 控件 源码

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共2页)

using System;

using System.Web.UI.HtmlControls;

using System.IO;

 

namespace Web.Custom

{

       /*

        * ----------------------------------------

        * 功能: 通过HtmlInputFile控件上传文件类

        * 创建: 2005-09-21

        * 作者: ∮明天去要饭

        * 邮箱: kgidwss@hotmail.com

        * ---------------------------------------

       */

 

       /// <summary>

       /// HtmlInputFileControl 的摘要说明。

       /// </summary>

       public class HtmlInputFileControl

       {

              #region HtmlInputFileControl

              public HtmlInputFileControl()

              {

                     //

                     // TODO: 在此处添加构造函数逻辑

                     //

              }

              #endregion

 

              #region IsAllowedExtension

              /// <summary>

              /// 是否允许该扩展名上传

              /// </summary>

              /// <param name="hifile">HtmlInputFile控件</param>

              /// <returns>允许则返回true,否则返回false</returns>

              public static bool IsAllowedExtension(HtmlInputFile hifile)

              {

                     string strOldFilePath = "",strExtension = "";

 

                     //允许上传的扩展名,可以改成从配置文件中读出

                     string[] arrExtension = {".gif",".jpg",".jpeg",".bmp",".png"};

 

                     if(hifile.PostedFile.FileName != string.Empty)

                     {

                            strOldFilePath = hifile.PostedFile.FileName;

                            //取得上传文件的扩展名

                            strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));

                            //判断该扩展名是否合法

                            for(int i = 0; i< arrExtension.Length; i++)

                            {

                                   if(strExtension.Equals(arrExtension[i]))

                                   {

                                          return true;

                                   }

                            }

                     }           

                     return false;

              }

              #endregion

 

              #region IsAllowedLength

              /// <summary>

              /// 判断上传文件大小是否超过最大值

              /// </summary>

              /// <param name="hifile">HtmlInputFile控件</param>

              /// <returns>超过最大值返回false,否则返回true.</returns>

              public static bool IsAllowedLength(HtmlInputFile hifile)

              {

                     //允许上传文件大小的最大值,可以保存在xml文件中,单位为KB

                     int i = 20;

            //如果上传文件的大小超过最大值,返回flase,否则返回true.

                     if(hifile.PostedFile.ContentLength > i * 1024)

                     {

                            return false;

                     }

                     return true;

              }

              #endregion

 

              #region SaveFile

              /// <summary>

              /// 上传文件并返回文件名

              /// </summary>

              /// <param name="hifile">HtmlInputFile控件</param>

              /// <param name="strAbsolutePath">绝对路径.:Server.MapPath(@"Image/upload")Server.MapPath(@"Image/upload/")均可,\符号亦可</param>

              /// <returns>返回的文件名即上传后的文件名</returns>

              public static string SaveFile(HtmlInputFile hifile,string strAbsolutePath)

              {

                     string strOldFilePath = "",strExtension = "",strNewFileName = "";

 

                     if(hifile.PostedFile.FileName != string.Empty)

                     {

                            strOldFilePath = hifile.PostedFile.FileName;

                            //取得上传文件的扩展名

                            strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));

                            //文件上传后的命名

                            strNewFileName = GetUniqueString() + strExtension;

                            if(strAbsolutePath.LastIndexOf("\\") == strAbsolutePath.Length)

                            {

                                   hifile.PostedFile.SaveAs(strAbsolutePath + strNewFileName);   

                            }

                            else

                            {

                                   hifile.PostedFile.SaveAs(strAbsolutePath + "\\" + strNewFileName); 

                            }

                     }

                     return strNewFileName;

              }

              #endregion

 

              #region CoverFile

              /// <summary>

              /// 重新上传文件,删除原有文件

              /// </summary>

              /// <param name="ffFile">HtmlInputFile控件</param>

              /// <param name="strAbsolutePath">绝对路径.:Server.MapPath(@"Image/upload")Server.MapPath(@"Image/upload/")均可,\符号亦可</param>

              /// <param name="strOldFileName">旧文件名</param>

              public static void CoverFile(HtmlInputFile ffFile,string strAbsolutePath,string strOldFileName)

              {

                     //获得新文件名

                     string strNewFileName = GetUniqueString();

 

                     if(ffFile.PostedFile.FileName != string.Empty)

                     {

                            //旧图片不为空时先删除旧图片

                            if(strOldFileName != string.Empty)

                            {                                

                                   DeleteFile(strAbsolutePath,strOldFileName);                                                               

                            }

                            SaveFile(ffFile,strAbsolutePath);

                     }                  

              }

              #endregion

 

              #region DeleteFile

              /// <summary>

              /// 删除指定文件

              /// </summary>

              /// <param name="strAbsolutePath">文件绝对路径</param>

              /// <param name="strFileName">文件名</param>

              public static void DeleteFile(string strAbsolutePath, string strFileName)

              {

                     if(strAbsolutePath.LastIndexOf("\\") == strAbsolutePath.Length)

                     {

                            if(File.Exists(strAbsolutePath + strFileName))

                            {                                

                                   File.Delete(strAbsolutePath + strFileName);

                            }

                     }

                     else

                     {                         

                            if(File.Exists(strAbsolutePath + "\\" + strFileName))

                            {                                

                                   File.Delete(strAbsolutePath + "\\" + strFileName);

                            }

                     }

              }

              #endregion

 

              #region GetUniqueString

              /// <summary>

              /// 获取一个不重复的文件名

              /// </summary>

              /// <returns></returns>

              public static string GetUniqueString()

              {                  

                     return DateTime.Now.ToString("yyyyMMddhhmmss");

              }

              #endregion

       }

}

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章