`

多文件上传

阅读更多
document.getElementById("MyFile")和insertAdjacentHTML
("beforeEnd", str)的含义和用法收藏
新一篇: 解决WIN2003因为限制ASP上传最大数值的方法 | 旧一篇: Web形式实现上传文件
document.getElementById("MyFile")为:获得名称为MyFile的层。
insertAdjacentHTML("beforeEnd", str)为:
MyFile层在其后追加插入内容为 INPUT type="file" size="50" NAME="File"> 的html格式信息。
向下面的例子,可以拷贝到网页中试试:
<script language="JavaScript">
function addFile()
{
var str = '<INPUT type="file" size="50" NAME="File">'
document.getElementById("MyFile").insertAdjacentHTML("beforeEnd", str)
}
</script>
<div id="MyFile">
1234567
</div>dfdfdfdf
<a href="javascript:addFile()">df</a>
发表于 @ 2006年05月25日 20:49:00|评论(0 )|编辑




using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class AddNews : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            this.BindNewsKind();
        }
    }
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        string NewsTitle = this.TxtNewsTitle.Text;
        int NewsKindId = Convert.ToInt32(this.DropNewsKinds.SelectedValue);
        string NewsBody = this.WebNewsBody.Text;
        DateTime PubTime = DateTime.Now;
      
        int UserId = Users.GetUserIdByUserName(Session["UserName"].ToString());
        News SingleNews = new News();
        SingleNews.NewsTitle = NewsTitle;
        SingleNews.NewsKindId = NewsKindId;
        SingleNews.NewsBody = NewsBody;
        SingleNews.PubDate = PubTime;
        SingleNews.UserId = UserId;
        if (News.AddNews(SingleNews))
        {
            this.LabMessage.Text = "添加成功!";
        }
        else
        {
            this.LabMessage.Text = "添加失败";
        }
    }
    public void BindNewsKind()
    {
        this.DropNewsKinds.DataSource = NewsKinds.GetAllNewsKinds();
        this.DropNewsKinds.DataTextField = "KindName";
        this.DropNewsKinds.DataValueField = "KindId";
        this.DropNewsKinds.DataBind();
        this.DropNewsKinds.SelectedIndex = 0;
    }


    protected void BtnCreatHtml_Click(object sender, EventArgs e)
    {
        string NewsTitle = this.TxtNewsTitle.Text;
        string NewsKindName = this.DropNewsKinds.SelectedItem.Text;
        string NewsBody = this.WebNewsBody.Text;
        DateTime PubTime = DateTime.Now;
        string UserName = Session["UserName"].ToString();
        Response.Write(NewsKindName);
        string[] strNewsHtml = new string[]{NewsTitle,NewsKindName,NewsBody,PubTime.ToString(),UserName};
        string[] strOldHtml = new string[] { "@Title", "@NewsKInd", "@NewsBody", "@PubTime", "@UserName" };
        string strFileName=DateTime.Now.ToString("ddhhmmss")+".html";
        string strFilePath = string.Format("NewsHtml/{0}",strFileName);
        try
        {
            if (Files.CreatHtmlPage(strNewsHtml, strOldHtml, "mode.htm", strFilePath))
            {
                this.LabMessage.Text = "生成成功!";
            }
            else
            {
                this.LabMessage.Text = "生成失败!";
            }
           
        }
        catch
        {
            this.LabMessage.Text = "生成失败!";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {   
        string[] strNewsHtml=new string[]{"看看我们生成的首页"};
        string[] strStartHtml=new string[]{"<!-- StartBody-->"};
        string[] strEndHtml=new string[]{"<!-- BodyEnd-->"};
        if (Files.UpdateHtmlPage(strNewsHtml, strStartHtml, strEndHtml, "NewsHtml/20074442.html"))
        {
            this.LabMessage.Text = "更新首页成功!";
        }
        else
        {
            this.LabMessage.Text = "更新首页失败!";
        }

    }
    protected void BtnFileUp_Click(object sender, EventArgs e)
    { if (FileUpload1.HasFile)
      {
        string FileName = FileUpload1.FileName;
        string strExPrentFile = FileName.Substring(FileName.LastIndexOf(".") + 1);
        string[] NoExPrentFile = new string[] { "exe", "asp", "php" };
        bool IsUp = true;
        for (int i = 0; i < NoExPrentFile.Length;i++ )
        {
            if(strExPrentFile.Equals(NoExPrentFile[i]))
            {
                IsUp = false;
            }
        }
        if(IsUp)
        {
            
                string sFilePath = string.Format("files/{0}.{1}", DateTime.Now.ToString("mmhhddss"), strExPrentFile);
                FileUpload1.SaveAs(Server.MapPath(sFilePath));
                this.LabMessage.Text = "文件上传成功!" + "<br />文件名是:" + string.Format("{0}.{1}", DateTime.Now.ToString("mmhhddss"), strExPrentFile) + "<br />文件的格式:" + strExPrentFile;
        }
        else
        {
            this.LabMessage.Text = string.Format("不允许上传{0}格式文件", NoExPrentFile);
        }
    }
        else
          {
                this.LabMessage.Text = "不能上传空文件";
          }
    }
    protected void BtnUpFiles_Click(object sender, EventArgs e)
    {
        StringBuilder strMessage = new StringBuilder();
        HttpFileCollection Files = HttpContext.Current.Request.Files;
        for (int i = 0; i < Files.Count;i++ )
        {
            HttpPostedFile PostedFile = Files[i];
            if (PostedFile.ContentLength>0)
            {
                string FileName = PostedFile.FileName;
                
                string strExPrentFile = FileName.Substring(FileName.LastIndexOf(".") + 1);
                string[] NoExPrentFile = new string[] { "exe", "asp", "php" };
                bool IsUp = true;
                for (int j = 0; j < NoExPrentFile.Length; j++)
                {
                    if (strExPrentFile.Equals(NoExPrentFile[j]))
                    {
                        IsUp = false;
                    }
                }
                if (IsUp)
                {

                    string sFilePath = string.Format("files/{0}.{1}", DateTime.Now.ToString("mmhhddss"), strExPrentFile);
                    PostedFile.SaveAs(Server.MapPath(sFilePath));
                    strMessage.Append("文件上传成功!");
                    strMessage.Append(string.Format("{0}.{1}", DateTime.Now.ToString("mmhhddss"), strExPrentFile));
                    strMessage.Append("<br />");
                    strMessage.Append("文件的格式:");
                    strMessage.Append(strExPrentFile);
                    strMessage.Append("<br />```````````````<br />");
                    //this.LabMessage.Text = "文件上传成功!" + "<br />文件名是:" + string.Format("{0}.{1}", DateTime.Now.ToString("mmhhddss"), strExPrentFile) + "<br />文件的格式:" + strExPrentFile;
                }
                else
                {
                    //this.LabMessage.Text = string.Format("不允许上传{0}格式文件", NoExPrentFile);
                }
            }
            else
            {
                //this.LabMessage.Text = "不能上传空文件";
            }
        }
        this.LabMessage.Text = strMessage.ToString();
    }
    protected void BtnShow_Click(object sender, EventArgs e)
    {
        string FileName = "00112145.ppt";
        string strExPrentFile = FileName.Substring(FileName.LastIndexOf(".") + 1);
        string[] NoExPrentFile = new string[] { "jpg", "fig", "jpeg","bmp" };
        bool IsUp = false;
        for (int j = 0; j < NoExPrentFile.Length; j++)
        {
            if (strExPrentFile.Equals(NoExPrentFile[j]))
            {
                IsUp = true;
            }
        }
        if (IsUp)
        {


           // this.LabMessage.Text = "文件上传成功!" + "<br />文件名是:" + string.Format("{0}.{1}", DateTime.Now.ToString("mmhhddss"), strExPrentFile) + "<br />文件的格式:" + strExPrentFile;
            this.LabFile.Text = string.Format("<img src='files/{0}' />",FileName);
        }
        else
        {
            this.LabFile.Text = string.Format("<a href='files/{0}'>{1}</a>",FileName,FileName);
        }
    }
}




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics