编写upload程序
<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>
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;
}
}
<section name="ITsolutionConfiguration" type="ITsolution.Config.ITsolutionConfig"/>
</configSections>
<ITsolutionConfiguration size="1024000" type="image/gif|image/pjpeg"/>
/// 分解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类
{
/// <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中设置属性值。