通过HtmlInputFile控件上传文件的类(转自小山BLOG)

本文介绍了一个用于ASP.NET的文件上传控制类,该类提供了文件扩展名验证、文件大小限制、文件保存等功能,并实现了文件覆盖及删除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.IO;
None.gif
None.gif
namespace Document.Bll
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class HtmlInputFileControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
HtmlInputFileControl#region HtmlInputFileControl
InBlock.gif        
public HtmlInputFileControl()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IsAllowedExtension是否允许该扩展名上传#region IsAllowedExtension是否允许该扩展名上传
InBlock.gif        
public static bool IsAllowedExtension(HtmlInputFile hifile)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strOldFilePath = "",strExtension = "";
InBlock.gif
InBlock.gif            
//允许上传的扩展名,可以改成从配置文件中读出
ExpandedSubBlockStart.gifContractedSubBlock.gif
            string[] arrExtension = dot.gif{".gif",".jpg",".jpeg",".bmp",".png"};
InBlock.gif
InBlock.gif            
if(hifile.PostedFile.FileName != string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strOldFilePath 
= hifile.PostedFile.FileName;
InBlock.gif                
//取得上传文件的扩展名
InBlock.gif
                strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
InBlock.gif                
//判断该扩展名是否合法
InBlock.gif
                for(int i = 0; i< arrExtension.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(strExtension.Equals(arrExtension[i]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return true;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
        
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IsAllowedLength判断上传文件大小是否超过最大值#region IsAllowedLength判断上传文件大小是否超过最大值
InBlock.gif        
public static bool IsAllowedLength(HtmlInputFile hifile)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//允许上传文件大小的最大值,可以保存在xml文件中,单位为KB
InBlock.gif
            int i = 20;
InBlock.gif            
//如果上传文件的大小超过最大值,返回flase,否则返回true.
InBlock.gif
            if(hifile.PostedFile.ContentLength > i * 1024)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
SaveFile上传文件并返回文件名#region SaveFile上传文件并返回文件名
InBlock.gif        
public static string SaveFile(HtmlInputFile hifile,string strAbsolutePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strOldFilePath = "",strExtension = "",strNewFileName = "";
InBlock.gif
InBlock.gif            
if(hifile.PostedFile.FileName != string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strOldFilePath 
= hifile.PostedFile.FileName;
InBlock.gif                
//取得上传文件的扩展名
InBlock.gif
                strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
InBlock.gif                
//文件上传后的命名
InBlock.gif
                strNewFileName = GetUniqueString() + strExtension;
InBlock.gif                
if(strAbsolutePath.LastIndexOf("\\"== strAbsolutePath.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    hifile.PostedFile.SaveAs(strAbsolutePath 
+ strNewFileName);    
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    hifile.PostedFile.SaveAs(strAbsolutePath 
+ "\\" + strNewFileName);    
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return strNewFileName;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
CoverFile重新上传文件,删除原有文件#region CoverFile重新上传文件,删除原有文件
InBlock.gif        
public static void CoverFile(HtmlInputFile ffFile,string strAbsolutePath,string strOldFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//获得新文件名
InBlock.gif
            string strNewFileName = GetUniqueString();
InBlock.gif
InBlock.gif            
if(ffFile.PostedFile.FileName != string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//旧图片不为空时先删除旧图片
InBlock.gif
                if(strOldFileName != string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    DeleteFile(strAbsolutePath,strOldFileName);                                        
ExpandedSubBlockEnd.gif                }

InBlock.gif                SaveFile(ffFile,strAbsolutePath);
ExpandedSubBlockEnd.gif            }
            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
DeleteFile删除指定文件#region DeleteFile删除指定文件
InBlock.gif        
public static void DeleteFile(string strAbsolutePath, string strFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(strAbsolutePath.LastIndexOf("\\"== strAbsolutePath.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(File.Exists(strAbsolutePath + strFileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    File.Delete(strAbsolutePath 
+ strFileName);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                
InBlock.gif                
if(File.Exists(strAbsolutePath + "\\" + strFileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    File.Delete(strAbsolutePath 
+ "\\" + strFileName);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetUniqueString获取一个不重复的文件名#region GetUniqueString获取一个不重复的文件名        
InBlock.gif        
public static string GetUniqueString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            
return DateTime.Now.ToString("yyyyMMddhhmmss");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/chinhr/archive/2007/06/28/798424.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值