











拖入fileupload控件及button按钮
<div>
<asp:FileUpload ID="m_file" runat="server" />
<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在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中设置属性值。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。