多图片上传

    <script language="JavaScript">
 function UploadImage(ex)
 {
  //document.Form1.Table1.innerText="<img src=" + document.Form1.File1.value; + " border=0>"
      //document.Form1.ImageInfo.src=document.Form1.File1.value; 175 165
  var e = document.getElementById("ImageInfo");
  if(ex.value!="")
  {
   
   var FileType = ex.value.substring(ex.value.lastIndexOf(".")+1);
   var ImageInfoText = "";
   if(FileType.toUpperCase() == "SWF")
   {
    ImageInfoText ="<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0'";
    ImageInfoText +=" width='112' height='72'>";
    ImageInfoText +="<param name='movie' value='";
    ImageInfoText +=ex.value;
    ImageInfoText += "'><param name='quality' value='high'><embed src='";
    ImageInfoText +=ex.value;
    ImageInfoText += "' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' ";
    ImageInfoText +=" width='112' height='72'>";
    ImageInfoText +="</embed></object>";
   }
   else
   {
    ImageInfoText ="<img src='" + ex.value + "' border=0 "
    ImageInfoText +=" width='99' height='99'>";
   }

   e.innerHTML=ImageInfoText;
  }
  else
  {
   e.innerHTML="";
  }
 }
 
 function imgLoad(strPicUrlPath1,strPicUrlPath2,strPicUrlPath3)
 {
     if(strPicUrlPath1!="")
     {
         document.getElementById('TestImg1').src=strPicUrlPath1;
     }
     else
     {
         document.getElementById('TestImg1').src="../Images/defaultpic.gif";
     }
          if(strPicUrlPath2!="")
     {
         document.getElementById('TestImg2').src=strPicUrlPath2;
     }
     else
     {
         document.getElementById('TestImg2').src="../Images/Silverpic.gif";
     }
          if(strPicUrlPath3!="")
     {
         document.getElementById('TestImg3').src=strPicUrlPath3;
     }
     else
     {
         document.getElementById('TestImg3').src="../Images/Goldpic.gif";
     }
     document.getElementById('imgurl1').value= strPicUrlPath1;
     document.getElementById('imgurl2').value= strPicUrlPath2;
     document.getElementById('imgurl3').value= strPicUrlPath3;
     checkLevel();
    
 }
 function checkLevel()
 {
    var imglevel=0;
    imglevel=getCookie("userlevel");
    if(imglevel<2)
    {
   document.getElementById('ImgFileUpload3').disabled=true;
    }
    if(imglevel<1)
    {
    document.getElementById('ImgFileUpload2').disabled=true;
    } 
    
 }
</script>

<div>

        <table width="100%" height="180" border="0" align="center" cellpadding="0" cellspacing="0">

                    <tr>
                      <td id="test"  align="center" valign="middle" style="width:33%"><img id="TestImg1" runat=server name="TestImg1" src="../Images/defaultpic.gif"  alt="搜金网欢迎你" width="93" height="93" />&nbsp;<br /></td>
                      <td id="testtd" align="center" valign="middle" style="width: 212px"><img id="TestImg2" runat=server src="../Images/Silverpic.gif" alt="搜金网欢迎你" width="93" height="93" />&nbsp;<br /></td>
                      <td align="center" valign="middle"><img id="TestImg3" runat=server src="../Images/Goldpic.gif" alt="搜金网欢迎你" width="93" height="93" />&nbsp;<br /></td>
  
                    </tr>
                    <tr>
                      <td align="center" valign="middle" style="width: 33%">
                          &nbsp;<INPUT id="ImgFileUpload1" type="file" name="ImgFileUpload1" runat="server"   onpropertychange="document.getElementById('TestImg1').src=this.value;" style="width: 155px; height: 23px">
                      </td>
                      <td align="center" valign="middle" style="width: 33%">
                          &nbsp;<INPUT id="ImgFileUpload2" type="file" name="ImgFileUpload2" runat="server"  onpropertychange="document.getElementById('TestImg2').src=this.value;" style="width: 155px; height: 23px">&nbsp;

                      </td>
                      <td align="center" valign="middle" style="width: 33%">
                          &nbsp;<INPUT id="ImgFileUpload3" type="file" name="ImgFileUpload3"   runat="server"  onpropertychange="document.getElementById('TestImg3').src=this.value;" style="width: 155px; height: 23px"> &nbsp;

                      </td>

                    </tr>

                </table>
                        <asp:Label ID="strStatus" runat="server" BorderColor="White" BorderStyle="None" Font-Bold="True"
            Font-Names="宋体" Font-Size="9pt" Width="500px"></asp:Label>
            </div>   

private bool SaveImages()
    {
        ///'遍历File表单元素
        HttpFileCollection files = HttpContext.Current.Request.Files;

        /// '状态信息
        System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
        // System.Text.StringBuilder strPicUrl = new System.Text.StringBuilder();
        strMsg.Append("上传的文件分别是:<hr color=red>");
        try
        {
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                ///'检查文件扩展名字
                HttpPostedFile postedFile = files[iFile];
                string fileNameClient, fileExtension;
                fileNameClient = System.IO.Path.GetFileName(postedFile.FileName);
                if (fileNameClient != "")
                {
                    if (postedFile.ContentType == "image/bmp" || postedFile.ContentType == "image/gif" || postedFile.ContentType == "image/pjpeg")
                    {
                        if (postedFile.ContentLength <= Comm.UpLoadMaxSize)
                        {

                            //string name = postedFile.PostedFile.FileName;                  // 客户端文件路径
                            FileInfo file = new FileInfo(fileNameClient);
                            string fileName = Comm.GetUniqueString() +  userid + GetRandom() + file.Name.Substring(file.Name.LastIndexOf("."));                                    // 文件名称
                            string strRelativeUrl = Comm.UpLoadPathUser + "/" + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + fileName;
                            string webPath = Server.MapPath("~" + Comm.UpLoadPathUser) + "/" + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/";
                            string webFilePath = webPath + fileName;        // 服务器端文件路径


                            //
                            fileExtension = System.IO.Path.GetExtension(fileName);

                            strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
                            strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
                            strMsg.Append("上传文件的文件名:" + fileNameClient + "<br>");
                            strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
                            ///'可根据扩展名字的不同保存到不同的文件夹
                            ///注意:可能要修改你的文件夹的匿名写入权限。
                            //postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);
                            //

                            if (!File.Exists(webFilePath))
                            {
                                try
                                {
                                    if (System.IO.Directory.Exists(webPath))
                                    {
                                        postedFile.SaveAs(webFilePath);
                                    }
                                    else
                                    {
                                        System.IO.Directory.CreateDirectory(webPath);
                                        postedFile.SaveAs(webFilePath);
                                    }
                                    //strPicUrl.Append(strRelativeUrl + ",");
                                    strPicUrlPath = strPicUrlPath + strRelativeUrl + ",";
                                    //lblView.Text = "提示:文件“" + fileName + "”成功上传,文件类型为:" + ImgFileUpload.PostedFile.ContentType + ",文件大小为:" + ImgFileUpload.PostedFile.ContentLength + "B";
                                    //return true;
                                }
                                catch (Exception ex)
                                {
                                    strStatus.Text = "提示:文件上传失败,失败原因:" + ex.Message;
                                    return false;
                                }
                            }
                            else
                            {
                                strStatus.Text = "提示:文件已经存在,请重命名后上传";
                                return false;
                            }
                        }
                        else
                        {
                            strStatus.Text = "提示:文件太大,请上传200kb以内";
                            return false;

                        }                      

                    }
                    else
                    {
                        strStatus.Text = "提示:文件类型不符";
                        return false;
                    }

                }
                else
                {
                    strPicUrlPath += "" + ",";
                }
            }

            //strStatus.Text = PICURL.ToString();
            return true;
        }
        catch (System.Exception Ex)
        {
            strStatus.Text = Ex.Message;
            return false;
        }
    }
    #region 取随机数
    private int GetRandom()
    {
        Random r = new Random();
        return r.Next();

    }
    #endregion 

你可能感兴趣的:(多图片上传)