.net文件上传类

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace NetRoute.Test_News.Comman
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class Common
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public Common()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 生成的文件名类别(guid,date两种)
        
/// </summary>

        public enum FileNameType
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Guid,
            DateTime,
        }

        
private static string _Message = string.Empty;//操作消息..
        public static string Message
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _Message; }
        }


        
static int Seed = 1;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 上传文件
        
/// </summary>
        
/// <param name="_fu">文件上传控件</param>
        
/// <param name="path">上传路径(不带文件名)</param>
        
/// <param name="FileSize">文件大小(以字节为单位)</param>
        
/// <param name="NoFile">是否允许空</param>
        
/// <param name="Type">生成的文件名类型(Guid/DateTime)</param>
        
/// <returns></returns>

        public static bool UploadFile(FileUpload _fu,string path,int? FileSize,bool NoFile,FileNameType Type)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
bool _result = true;
            
if (_fu.HasFile)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (FileSize != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if (_fu.PostedFile.ContentLength > FileSize)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        _Message 
= "上传的文件过大!";
                        _result 
= false;
                        
return _result;
                    }

                }

                
string FileName = _fu.FileName;
                
string FileType = _fu.PostedFile.ContentType;
                
string extend = FileName.Substring((FileName.LastIndexOf('.'+ 1));
                
if (FileType.Substring(0,FileType.IndexOf('/')) == "image")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
if (Type == FileNameType.Guid)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
string NewFileName = System.Guid.NewGuid().ToString() + "." + extend;
                            _fu.SaveAs(path 
+ NewFileName);
                        }

                        
else if (Type == FileNameType.DateTime)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            Random rd 
= new Random(Seed);
                            
string NewFileName = MakeFileName + "." + extend;
                            _fu.SaveAs(path 
+ NewFileName);
                        }

                    }

                    
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        _Message 
= ex.Message;
                        _result 
= false;
                    }

                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    _Message 
= "上传的文件格式不正确!";
                    _result 
= false;
                }

            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (!NoFile)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    _result 
= false;
                    _Message 
= "您没有上传文件!";
                }


            }

            
return _result;
        }


        
private static int _Seed = 0;
        
private static string MakeFileName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                System.Random ran 
= new Random(_Seed);
                _Seed
++;
                
if (_Seed == 10000)
                    _Seed 
= 0;
                
return DateTime.Now.ToFileTime().ToString() + ran.Next(1999).ToString();
            }

        }

        
public static bool UploadFile(FileUpload _fu, string path, bool NoFile, FileNameType Type)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return UploadFile(_fu, path, null, NoFile, Type);
        }

    }

}

转载于:https://www.cnblogs.com/danielduan/archive/2009/01/20/1378742.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值