惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
Jina AI
Jina AI
雷峰网
雷峰网
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
美团技术团队
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
博客园 - Franky
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
量子位
IT之家
IT之家
人人都是产品经理
人人都是产品经理
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - laifangsong

请问福州哪里卖打折计算机图书,又可按原价开发票的? 关于目录访问的iis配置问题 福州招聘asp程序员 qq聊天的小秘密 吃鱼引发的问题和中国式管理 为什么点击flash链接到本页面,Request.Referrer将无法获得url来源 观察。总结 - 思想(一) 在csdn上看到奶牛问题,写了下算法 C#(1.1)邮件发送类,功能全面,调用灵活、方便 思考_070614 asp中JMail(4.4)发送邮件 iis6(win2003)中的aspnet_client和iis5,5.1(win2000,winxp)不一样,系统迁移时一定要注意。 看似诡异的session赋值错误 不错的分页存储过程(转) 矩阵相乘(c) 委托:两个例子(主人仆人、打游戏) 取奇数游戏(c) 用递归算法求和为指定值N的所有组合 汉若塔问题(c)
单个文件上传类(可以自定义配置)
laifangsong · 2007-05-30 · via 博客园 - laifangsong

调用可以很灵活,代码如下:

using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Text;
using System.IO;namespace aspnet_test
{
    
/// <summary>
    
/// 单个文件上传类
    
/// 
    
/// 可以自定义:上传目录,上传文件类型、大小,是否使用随机上传文件名。
    
/// 
    
/// 作者:laifangsong  QQ:25313644  GMail:laifangsong@gmail.com
    
/// 原文:http://www.cnblogs.com/jiny-z/archive/2007/05/30/764762.html
    
/// </summary>
    public class UploadFile_Single
    {
        
/* web.config设置:
         * <appSettings>        
         *     <add key="uploadDir" value="UploadFiles/" />
         *   <add key="uploadFileExt" value="gif|jpg|jpeg|png|bmp|doc|xls|hlp|chm|rar|zip" />    
         *   <add key="uploadFileSize" value="2048" />
         *   <add key="isUseRandFileName" value="true" />            
         * </appSettings>
         * 
         * private string uploadDir = ConfigurationSettings.AppSettings["uploadDir"];;
         * private string uploadFileExt = ConfigurationSettings.AppSettings["uploadFileExt"];
         * private int uploadFileSize = int.Parse(ConfigurationSettings.AppSettings["uploadFileExt"]);
         * private bool isUseRandFileName = bool.Parse(ConfigurationSettings.AppSettings["uploadFileExt"]);
         * 
*///上传目录,相对于本文件路径;以“/”结尾;如果目录不存在,会自动创建;如果要启用绝对路径,写法:“/虚拟目录名称/上传文件目录名称/”
        private string uploadDir = "UploadFiles/";
        
//允许上传文件类型,逗号隔开
        private string uploadFileExt = "gif|jpg|jpeg|png|bmp|doc|xls|hlp|chm|rar|zip";
        
//允许上传文件大小,k 为单位
        private int uploadFileSize = 2048;
        
//是否使用随机上传文件名;如果不使用,会覆盖上传目录的同名文件
        private bool isUseRandFileName = true;//上传结果
        private string uploadResult = "";
        
//上传文件保存路径
        private string uploadPath = "";
        
        
public string UploadDir
        {
            
get
            {
                
return uploadDir;
            }
            
set
            {
                uploadDir 
= value;
            }
        }
        
public string UploadFileExt
        {
            
get
            {
                
return uploadFileExt;
            }
            
set
            {
                uploadFileExt 
= value;
            }
        }
        
public int UploadFileSize
        {
            
get
            {
                
return uploadFileSize;
            }
            
set
            {
                uploadFileSize 
= value;
            }
        }
        
public bool IsUseRandFileName
        {
            
get
            {
                
return isUseRandFileName;
            }
            
set
            {
                isUseRandFileName 
= value;
            }
        }
        
public string UploadResult
        {
            
get
            {
                
return uploadResult;
            }
        }
        
public string UploadPath
        {
            
get
            {
                
return uploadPath;
            }
        }        
//上传文件方法
        public bool Upload(Page page)
        {
            HttpPostedFile uploadFile 
= page.Request.Files[0];
            
//获取上传文件属性
            string fileFullName = uploadFile.FileName;
            
string contentType = uploadFile.ContentType;
            
int contentLength = uploadFile.ContentLength;
            
//取得文件名
            string fileName = System.IO.Path.GetFileName(fileFullName);
            
//取得扩展名
            string fileExt = System.IO.Path.GetExtension(fileName).Remove(01);
            
//检测文件类型
            if( ("|"+UploadFileExt+"|").IndexOf( ("|"+fileExt+"|") ) < 0 )
            {
                uploadResult 
= "<font color='red'><b>上传文件格式错误!允许上传文件类型(" + uploadFileExt + ")</b></font>";
                
return false;
            }
            
//检测文件大小
            if(contentLength<1 || contentLength>uploadFileSize*1024)
            {
                uploadResult 
= "<font color='red'><b>上传文件大小错误!最大允许上传 " + uploadFileSize + " KB</b></font>";
                
return false;                
            }
            
//如果上传目录不存在,创建目录
            if(!uploadDir.EndsWith("/")) uploadDir += "/";
            
string tempPath = "";
            
string tempMapPath = "";
            
string tempDir = uploadDir.Substring(0,1)!="/" ? uploadDir : uploadDir.Substring(uploadDir.IndexOf("/"2)+1) ;
            
while(tempDir.IndexOf("/")>=0)
            {
                tempPath 
+= tempDir.Substring(0, tempDir.IndexOf("/")+1);            
                tempMapPath 
=  System.Web.HttpContext.Current.Server.MapPath(tempPath);
                
if(!Directory.Exists(tempMapPath))
                {
                    Directory.CreateDirectory(tempMapPath);
                }
                tempDir 
= tempDir.Substring(tempDir.IndexOf("/")+1);
            }
            
//随机文件名
            if(isUseRandFileName)
            {
                fileName 
= DateTime.Now.ToString("yyyyMMddhhmmss"+ (new Random()).Next(1001000).ToString() + "." + fileExt;
            }
string saveFilePath =  System.Web.HttpContext.Current.Server.MapPath(uploadDir) + @"\" + fileName;            
            uploadFile.SaveAs(saveFilePath);

            uploadPath 

= uploadDir+fileName;
            
//SaveUploadPathToDB(uploadPath);

            uploadResult 
+= "<font color='blue'><b>文件上传成功!</b></font>" + "<br/><br/>" +
                
"<b>保存位置:</b>" + uploadPath;
    
            
return true;
        }
//将上传文件路径存入数据库
        private void SaveUploadPathToDB(string uploadPath)
        {
            
//string sql = "update table 1 set f_uploadpath='" & uploadPath & "' where ";
            
//TSQL.ExecuteNonQuery(sql, null);
        }

    }
}


调用:

UploadFile_Single upfile = new UploadFile_Single();
upfile.Upload(
this);

            UploadFile_Single upfile = new UploadFile_Single();
            upfile.UploadDir 
= "UploadFiles/";
            upfile.UploadFileExt 
= "gif|jpg|jpeg|png|bmp";
            upfile.UploadFileSize 
= 1024;
            upfile.IsUseRandFileName 
= true;
            upfile.Upload(
this);

            UploadFile_Single upfile = new UploadFile_Single();
            upfile.UploadDir 
= "/虚拟目录名称/UploadFiles/1/2/3/";
            upfile.UploadFileExt 
= "gif|jpg|jpeg|png|bmp";
            upfile.UploadFileSize 
= 1024;
            upfile.IsUseRandFileName 
= true;
            upfile.Upload(
this);
               
               Response.Write(upfile.UploadResult); //上传结果信息
               Response.Write(upfile.UploadPath); //上传保存路径

(上传目录可以用绝对路径,也可用相对路径;如果上传目录不存在,会自动创建)