读取xml文件,上传固定格式的文件

本文介绍了一个使用ASP.NET实现文件上传的例子,包括图片和文档的上传,并通过XML文件来配置允许上传的文件类型。

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));
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值