xml 文件要求如下,下面代码必须手写.
<?xml version="1.0" encoding="utf-8" ?>
<fileset>
<upload></upload>
<fileformat>.jpg|.JPG|</fileformat>
<upload1>.doc|.html|.xls</upload1>
</fileset>
aspx页面
<form>
<table>
<tr>
<td><span style="width: 11%">照片:</span></td>
<td colspan="3"><input id="zhaopian" name="zhaopian" type="file" runat="server" /></td>
</tr>
<tr>
<td><span style="width: 11%">个人简历:</span></td>
<td colspan="3"><input id="gerenjianli" name="gerenjianli" type="file" runat="server" /></td>
</tr>
<tr>
<td colspan="4" style="text-align: center;">
<input type="button" value=" 添 加 " id="submits" style="height: 25px; line-height: 22px;"
onserverclick="submits_ServerClick" runat="server" />
<input onclick="location.reload();" style="height: 25px; line-height: 22px;" type="button"
value=" 重 写 " /></td>
</tr>
</table>
</form>
.aspx.cs页面
//引入空间
using System.IO;
using System.Xml;
//按钮buttron的点击事件
if (zhaopian.PostedFile.FileName != "")
{
XmlDocument document = new XmlDocument();
document.Load(Server.MapPath("UploadFile.xml"));
XmlNodeList cn = document.SelectNodes("fileset");//取图片
string strfileExt = cn.Item(0).SelectSingleNode("fileformat").InnerText.ToString();
if (strfileExt.IndexOf(System.IO.Path.GetExtension(zhaopian.PostedFile.FileName)) == -1)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language='javascript'>alert('只支持.jpg文件上传。');</script>");
return;
}
imgstrFileName = "picture" + "/" + System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Millisecond.ToString() + System.IO.Path.GetExtension(zhaopian.PostedFile.FileName);
DirectoryInfo directoryinfo = new DirectoryInfo(Server.MapPath("picture"));
if (!directoryinfo.Exists)
{
directoryinfo.Create();
}
zhaopian.PostedFile.SaveAs(Server.MapPath(imgstrFileName));
}
if (gerenjianli.PostedFile.FileName != "")
{
//string jianlistrFileName = "";
XmlDocument document = new XmlDocument();
document.Load(Server.MapPath("UploadFile.xml"));
XmlNodeList cn = document.SelectNodes("fileset");//取简历
string strfileExt = cn.Item(0).SelectSingleNode("upload1").InnerText.ToString();
if (strfileExt.IndexOf(System.IO.Path.GetExtension(gerenjianli.PostedFile.FileName)) == -1)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language='javascript'>alert('只支持.doc.xls.html文件上传。');</script>");
return;
}
jianlistrFileName = "jianli" + "/" + System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Millisecond.ToString() + System.IO.Path.GetExtension(gerenjianli.PostedFile.FileName);
DirectoryInfo directoryinfo = new DirectoryInfo(Server.MapPath("jianli"));
if (!directoryinfo.Exists)
{
directoryinfo.Create();
}
zhaopian.PostedFile.SaveAs(Server.MapPath(jianlistrFileName));
}
本文介绍了一个使用ASP.NET实现文件上传的例子,包括图片和文档的上传,并通过XML文件来配置允许上传的文件类型。
3152

被折叠的 条评论
为什么被折叠?



