水丝游云

跌倒了?就顺便看看绽放着蔚蓝色微笑的天空吧!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

多文件上传记录

Posted on 2007-11-28 22:58  水丝游云  阅读(443)  评论(0编辑  收藏  举报
由于时间紧迫无法实现类似126信箱的那种文件上传方法,可是总体的框架已经实现了,接下来的就是完善他,
记录下来,有空晚上他。
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="WebForm2.aspx.cs" Inherits="GloryView.Meeting.Web.Module.admin.MeetingManage.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" method="post" runat="server" enctype="multipart/form-data">
        <table align="center" style="width: 549px">
            <tr>
                <td align="center" style="height: 60px">
                    <h1>
                        多附件上传
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></h1>
                </td>
            </tr>
            <tr>
                <td id="TD">
                    <button style="width: 79px; height: 20px" type="button" onmouseover="fileAttachmentclick()">
                        继续添加</button>
                    <br />
              
                    <%--<div>
                        <div style='display: none'>
                            <input type="file" id="files" name="files" style='filter: alpha(opacity=0); width: 30px;
                                position: absolute' onchange='addFiles(this)'>
                        </div>
                    </div>--%>
                    <%-- <input  type="file" name="files" id="files" style="FILTER: alpha(opacity=0); POSITION: absolute; left: 205px; top: 80px;" onChange="addFiles(this)" size="1"><br />--%>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblError" runat="server"></asp:Label>
                    <input id="files1" name="files1" type="file" /></td>
            </tr>
            <tr>
                <td align="center" style="height: 26px">
                    <asp:Button ID="btnUpLoad" runat="server" Text=" 上 传 " EnableViewState="False" CausesValidation="true"
                        OnClick="btnUpLoad_Click" OnLoad="btnUpLoad_Load"></asp:Button>
                </td>
            </tr>
        </table>

        <script language="javascript">
 var count=0;
    function addFiles(file)
    {
//           if( file.value!="")
//           {
//                var sLineHTML="<div><div style='display:none'><input type='file' name='files'  id='files'     style='FILTER: alpha(opacity=0); WIDTH: 30px; POSITION: absolute'  onChange='addFiles(this)'></div><input type='button' onclick='javascript:delFileInput(this)' value='删除'></div>";
//                document.all["TD"].insertAdjacentHTML('beforeEnd',sLineHTML);
//           }
//        if(file.value=="")
//        {
//             var divToDel=oInputButton.parentNode;
//        divToDel.parentNode.removeChild(divToDel);
//        }
    }
    function delFileInput(oInputButton)
    {
        var divToDel=oInputButton.parentNode;
        divToDel.parentNode.removeChild(divToDel);
    }
    function fileAttachmentclick()
    {
   
        var obj;
         var f=document.getElementsByName("files");
       
        if(f.length==0)
        {
               count+=1;
               var sLineHTML="<div><div style='display:none'><input type='file' name='files'  id='files'     style='FILTER: alpha(opacity=0); WIDTH: 30px; POSITION: absolute'  onChange='addFiles(this)'></div><span id='Label1'></span><input type='button' onclick='javascript:delFileInput(this)' value='删除'></div>";
                document.all["TD"].insertAdjacentHTML('beforeEnd',sLineHTML);
              
         }
        else
        {
           obj= f[f.length-1];
            if(obj.value!="")
           {
                count+=1;
                var sLineHTML="<div><div style='display:none'><input type='file' name='files'  id='files'     style='FILTER: alpha(opacity=0); WIDTH: 30px; POSITION: absolute'  onChange='addFiles(this)'></div><input type='button' onclick='javascript:delFileInput(this)' value='删除'></div>";
                document.all["TD"].insertAdjacentHTML('beforeEnd',sLineHTML);
           }          
        }
        f=document.getElementsByName("files");
        obj= f[f.length-1];  
        with(obj)
        {
                style.posTop=event.srcElement.getBoundingClientRect().top
                var x=event.x-offsetWidth/2
                if(x<event.srcElement.getBoundingClientRect().left)x=event.srcElement.getBoundingClientRect().left
                if(x>event.srcElement.getBoundingClientRect().left+event.srcElement.offsetWidth-offsetWidth)x=event.srcElement.getBoundingClientRect().left+event.srcElement.offsetWidth-offsetWidth
                style.posLeft=x
                parentNode.style["display"]="block";
        }
 
}  
                   
                      
function fileAttachmentclick1(obj)
{
     with(obj)
     {
        style.posTop=event.srcElement.getBoundingClientRect().top
         var x=event.x-offsetWidth/2
         if(x<event.srcElement.getBoundingClientRect().left)x=event.srcElement.getBoundingClientRect().left
        if(x>event.srcElement.getBoundingClientRect().left+event.srcElement.offsetWidth-offsetWidth)x=event.srcElement.getBoundingClientRect().left+event.srcElement.offsetWidth-offsetWidth
        style.posLeft=x
    }
}  
        </script>

    </form>
</body>
</html>

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 Webb.WAVE.Controls.Upload;
using System.IO;
namespace GloryView.Meeting.Web.Module.admin.MeetingManage
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUpLoad_Load(object sender, EventArgs e)
        {

            Button m_button = sender as Button;
            WebbUpload m_upload = new WebbUpload();
            m_upload.RegisterProgressBar(m_button);
         

        }

        private void UploadFiles(string meetingID)
        {
 
            string m_path = Path.Combine(MapPath("~"), "MeetingAffix""" + DateTime.Now.Year.ToString() + """" + DateTime.Now.Month.ToString() + """" + meetingID);
            //Response.Write(m_path);
            WebbUpload m_upload = new WebbUpload();
            UploadFileCollection m_files = m_upload.GetUploadFileList("files");
            string m_filePath = string.Empty;

          //  this.Label1.Text = "Uploaded file:<br/>";
            foreach (UploadFile m_file in m_files)
            {
                //每上传成功一个文件就插入数据库一个跳记录
                //如果失败,则退出,提示用户上传文件失败,让用户就如修改界面进行重新上传文件
                if (m_file.FileName == null || m_file.FileName == string.Empty) continue;    //skip the empty input file.
                m_filePath = Path.Combine(m_path, m_file.FileName);
                m_file.SaveAs(m_filePath);
              //  this.Label1.Text += "<a href='""uploadedFiles""" + m_file.FileName + "'>uploadedFiles""" + Path.GetFileName(m_file.ClientFullPathName) + "</a><p>";
            }

            m_files = m_upload.GetUploadFileList("saomiaoFile");
            m_filePath = string.Empty;

            //this.Label2.Text = "Uploaded file:<br/>";
            foreach (UploadFile m_file in m_files)
            {
                if (m_file.FileName == null || m_file.FileName == string.Empty) continue;    //skip the empty input file.
                m_filePath = Path.Combine(m_path, m_file.FileName);
                m_file.SaveAs(m_filePath);
                //this.Label2.Text += "<a href='""uploadedFiles""" + m_file.FileName + "'>uploadedFiles""" + Path.GetFileName(m_file.ClientFullPathName) + "</a><p>";
            }
        }

        protected void btnUpLoad_Click(object sender, EventArgs e)
        {
            UploadFiles(Guid.NewGuid().ToString());
        }

    }
}

使用了博客园中的开源的大文件上传组件,没有完全调同,可是总体思路已经差不多了,有时间完善他。
现在系统的第一个版本用了一个比126信箱简单多的方法,可是也能搞定问题了。