asp.net2005+javascript+SqlServer2000

爱她就要好好地呵护她

 

多个上传(继续添加)参考

首先在添加脚本   
<script type="text/javascript">
   function addFile()
  {
      var str = '<BR><INPUT type="file" size="50" NAME="File" runat="server">'
      document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
  }
    </script>

<table class="fullwidth" align="center" width="100%">
                        <TR>
        <TD vAlign="top">
            导入运输单 :</TD>
        <TD>
         <P id="MyFile"><input id="filMyFile" type="file" size="50" name="filMyFile">&nbsp;<input onclick="addFile()" type="button" value="继续添加"></P>
         <asp:label id="lblAttachmentError" runat="server" ForeColor="Red"></asp:label><BR>
         <asp:button id="btnUpload" runat="server" Text="数据导入" OnClick="btnUpload_Click"></asp:button>
         <asp:label id="lblAttachment" runat="server"></asp:label>
            </TD>
       </TR>
</table>
很有意思的哦,也很实用的,下面写cs代码

protected void btnUpload_Click(object sender, EventArgs e)
    {
        wrongMsg = "";
        this.lblAttachment.Text = "";
        string strFilePath1 = "C:\\Uploads";//保存运输单的路径
        try
        {
            if (!Directory.Exists(strFilePath1))
                Directory.CreateDirectory(strFilePath1);
        }
        catch (Exception ex)
        {
            this.lblAttachmentError.Text+="错误:" + ex.Message;
            return;
        }
        // 可以参考的网址:http://topic.csdn.net/t/20021022/15/1115036.html
        HttpFileCollection files = HttpContext.Current.Request.Files;
        for (int i = 0; i < files.Count; i++)
        {
            if (i < files.Count)
            {
                if (files[i].FileName != "" || files[i] != null)
                {
                    int FileSize = 6 * 1024 * 1024;
                    HttpPostedFile myFile = files[i];
                    string strFilePath = myFile.FileName.ToString().Trim();//文件物理路径
                    string strExt = System.IO.Path.GetExtension(myFile.FileName);//文件格式
                    this.lblAttachmentError.Text = "<" + strFilePath + ">"; // Show file name
                    int nFindSlashPos = strFilePath.Trim().LastIndexOf("\\") + 1;//取最后的文件名字
                    string UploadFileName = strFilePath.Substring(nFindSlashPos);//运输单名称
                    string strSaveName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + UploadFileName;//上传后的文件名称(时间)
                    string FileName = UploadFileName;//this.txtWorkOrder.Text + "_" + string.Format("{0:yyMMdd-hhmmss}", DateTime.Now) + "_" + UploadFileName;
                    if (myFile.FileName.Trim() == "") // Empty value in Browse Box
                    {
                        wrongMsg = "<br>请选择运输单文件";
                        this.lblAttachmentError.Text = wrongMsg;
                        return;
                    }
                    //格式判断
                    if (strExt != ".xls")
                    {
                        this.lblAttachmentError.Text = "格式错误,所上传的文件格式务必为 Excel 文件.";
                        return;
                    }
                    if (myFile.ContentLength != 0)
                    {
                        if (myFile.ContentLength > FileSize)
                        {
                            this.lblAttachmentError.Text = "每个运输单限制在 6 MB 以内.";
                            return;
                        }
                        try
                        {
                            strFilePath1 = Server.MapPath(Request.ApplicationPath) + "\\OrderFolder\\" + strSaveName;//"C:\\Uploads\\" + FileName;
                            myFile.SaveAs(strFilePath1);
                        }
                        catch (Exception ex)
                        {
                            this.lblAttachmentError.Text += "错误:" + ex.Message;
                        }
                         ///这是我的特别代码了,可以不用了
                        //调用导入Excel函数
                        this.fetchExcelContent(strFilePath1, FileName, strSaveName);
                        //this.fetchExcelContent(this.FileUpload1.PostedFile.FileName);


                        //       string s=this.Request.PhysicalApplicationPath.ToString().Trim();
                        //       string s1=this.Request.ApplicationPath.ToString().Trim();
                        //       string s3=this.Server.MapPath("");
                        //myFile.SaveAs(this.Request.PhysicalApplicationPath.ToString().Trim() + @"\uploads\" + FileName);\\保存文件
                        //ArrayFileName[i] = FileName;
                        //       return;
                    }
                    else
                    {
                        this.lblAttachmentError.Text = "请选择运输单文件.";
                        return;
                    }
                }
            }
            //else
            //    this.lblAttachmentError.Text = "运输单数量超过限制(10个以内).";
        }
    }

posted on 2007-05-12 11:54  g無s所p畏   阅读(161)  评论(0)    收藏  举报

导航