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

推荐订阅源

小众软件
小众软件
A
About on SuperTechFans
博客园 - Franky
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
B
Blog
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
Martin Fowler
Martin Fowler
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 叶小钗
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
Last Week in AI
Last Week in AI
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
V
V2EX
G
Google Developers Blog

博客园 - Sunny

开发人员一定要加入收藏夹的网站(转载) 我写的一个小程序 tabs研究 dotnetnuke( modules /tabs )权限研究 数据库知识2 数据库学习1 asp.net缓存、企业程序库缓存及格式化日期 - Sunny - 博客园 可爱的外甥女 dotnetnuke 里profile 的使用 怎样写自己的ConfigurationHandler(DOTNETNUKE 研究)? 多国技术总结 远程教育系统总结 项目(多国技术) 总结 循环遍历一个控件方法 页面验证码程序(C#) string与stringbuilder flyweigth享元设计模式与委托总结 dotnetnuke 本地化技术杂谈 怎样获取获取GridView里面的label
编写upload程序
Sunny · 2006-04-05 · via 博客园 - Sunny

拖入fileupload控件及button按钮

    <div>
        
<asp:FileUpload ID="m_file" runat="server" />&nbsp;
        
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        
<br />
        
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    
</div>

button的click事件

    ITsolutionConfig isc = (ITsolutionConfig)ConfigurationManager.GetSection("ITsolutionConfiguration");
    protected void Page_Load(object sender, EventArgs e)
    {

    }   
   
protected void Button1_Click(object sender, EventArgs e)
    {
        ArrayList al 
= shared.splitSomebody(isc.Type);
        
int size = isc.Size;
        
if (m_file.PostedFile.ContentLength > size || !al.Contains(m_file.PostedFile.ContentType))
        {
            Response.Write(
"<script lauguage='javascript'>alert('你上传的图片太大或类型不符合!');history.back();</script>");

        }

else
        {
            m_file.SaveAs(Server.MapPath(
"."+ "\\upload\\" + m_file.FileName);
            Label1.Text 
= "上传成功!";
            Label1.ForeColor 
= System.Drawing.Color.Red;
        }
    }

在web.config文件中设置ITsolutionConfiguration节点

<configSections>
    
<section name="ITsolutionConfiguration" type="ITsolution.Config.ITsolutionConfig"/>
  
</configSections><ITsolutionConfiguration size="1024000" type="image/gif|image/pjpeg"/>

shared.splitSomebody(isc.Type)方法如下:

        /// <summary>
        
/// 分解somebody字符放入一个ArrayList中。
        
/// 例如:image/gif|image/pjpeg,以"|"为分割点,放入ArrayList当中。
        
/// </summary>
        public static ArrayList SplitSomebody(string somebody)
        {
            ArrayList al 
= new ArrayList();
            
string[] alist = somebody.Split('|');
            
foreach (string arlist in alist)
            {
                al.Add(arlist);
            }
            
return al;
        }

就这么多,我们可以在web.config中,设置上传文件的大小与类型。

怎样设置ITsolutionConfiguration节点的呢?
添加一个ITsolutionConfig类

namespace ITsolution.Config
{
    
/// <summary>
    
/// 设置web.config文件ITsolutionConfiguration节点属性值
    
/// </summary>
    public class ITsolutionConfig : ConfigurationSection
    {
        
public ITsolutionConfig()
        {
            
//
            
// TODO: Add constructor logic here
            
//
        }
        [ConfigurationProperty(
"size")]
        
public int Size
        {
            
get { return (int)this["size"]; }
            
set { this["size"= value; }
        }
        [ConfigurationProperty(
"type")]
        
public string Type
        {
            
get { return (string)this["type"]; }
            
set { this["type"= value; }
        }

    }
}

然后我们就可以在web.config文件中注册,就可以用了,如果属性要增加
1:在ITsolutionConfig类中增加属性
2:在web.config中设置属性值。