实现文件的上传

public class Upload
    {
        
private System.Web.HttpPostedFile postedFile = null;
        
private string savePath = "";
        
private string extension = "";
        
private int fileLength = 0;
        
private string filename = "";
        
/// <summary>
        
/// 上传组件
        
/// </summary>
        public System.Web.HttpPostedFile PostedFile
        {
            
get
            {
                
return postedFile;
            }
            
set
            {
                postedFile 
= value;
            }
        }
        
/// <summary>
        
/// 保存路径
        
/// </summary>
        public string SavePath
        {
            
get
            {
                
if (savePath != ""return savePath;
                
return "c://";
            }
            
set
            {
                savePath 
= value;
            }
        }
        
/// <summary>
        
/// 文件大小
        
/// </summary>
        public int FileLength
        {
            
get
            {
                
if (fileLength != 0return fileLength;
                
return 1024;
            }
            
set
            {
                fileLength 
= value * 1024;
            }
        }
        
/// <summary>
        
/// 文件护展名
        
/// </summary>
        public string Extension
        {
            
get
            {
                
if (extension != "")
                    
return extension;
                
return "txt";
            }
            
set
            {
                extension 
= value;
            }
        }
        
/// <summary>
        
/// 文件名
        
/// </summary>
        public string FileName
        {
            
get
            {
                
return filename;
            }
            
set
            {
                filename 
= value;
            }
        }
        
public string PathToName(string path)
        {
            
int pos = path.LastIndexOf(@"/");
            
return path.Substring(pos + 1);
        }
        
/// <summary>
        
/// 上传文件
        
/// </summary>
        
/// <returns></returns>
        public string UploadStart()
        {
            
bool tf = false;
            
string returnvalue = "";
            
if (PostedFile != null)
            {
                
try
                {
                    
string fileName = PathToName(PostedFile.FileName);
                    
if (filename != "")
                    {
                        fileName 
= filename;
                    }
                    
string _fileName = "";

                    
string[] Exten = Extension.Split(',');
                    
if (Exten.Length == 0)
                    {
                        returnvalue 
= "你未设置上传文件类型,系统不允许进行下一步操作!";
                    }
                    
else
                    {
                        
for (int i = 0; i < Exten.Length; i++)
                        {
                            
if (fileName.ToLower().EndsWith(Exten[i].ToLower()))
                            {
                                
if (PostedFile.ContentLength > FileLength)
                                {
                                    returnvalue 
= "上传文件限制大小:" + FileLength / 1024 + "kb!";
                                }
                                
string IsFileex = SavePath + @"/" + fileName;
                                
if (!Directory.Exists(SavePath)) { Directory.CreateDirectory(SavePath); }
                                PostedFile.SaveAs(IsFileex);
                                _fileName 
= fileName;
                                tf 
= true;
                                returnvalue 
= IsFileex ;
                            }
                        }
                        
if (tf == false)
                            returnvalue 
= "只允许上传" + Extension + " 文件!";
                    }
                }
                
catch (System.Exception exc)
                {
                    returnvalue 
= exc.Message;
                }
            }
            
else
            {
                returnvalue 
= "上文件失败!";
            }
            
return returnvalue;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值