上传多个图片文件

<script language="VBScript" type="text/vbscript"> NowFileInputNo = 1 NowFileInputCount = 0 Sub AddFileInput() If NowFileInputCount < 5 Then HTMLSrc = "

" HTMLSrc = HTMLSrc & "
   
"& vbcrlf document.all.add_line.InsertAdjacentHTML "beforeBegin", HTMLSrc NowFileInputNo = NowFileInputNo + 1 NowFileInputCount = NowFileInputCount + 1 Else Alert "增加太多文件" End IF End Sub Sub DelFileInput(tableno) document.all("FileInput"& tableno).outerHTML = "" NowFileInputCount = NowFileInputCount - 1 End Sub </script>  <script language="VBScript" type="text/vbscript">
   NowFileInputNo = 1
   NowFileInputCount = 0
   Sub AddFileInput()
   If NowFileInputCount < 5 Then
   HTMLSrc = "<table Id=FileInput"& NowFileInputNo &"><tr>"
   HTMLSrc = HTMLSrc & "<td><input type='file' name='file"& NowFileInputNo &"' class='bk' size='30'>&nbsp;&nbsp;&nbsp;<input type='button' value='删除' class='bk' OnClick=""DelFileInput("& NowFileInputNo &")""></td></tr></table>"& vbcrlf

   document.all.add_line.InsertAdjacentHTML "beforeBegin", HTMLSrc
   NowFileInputNo = NowFileInputNo + 1
   NowFileInputCount = NowFileInputCount + 1
   Else
   Alert "增加太多文件"
   End IF
   End Sub

   Sub DelFileInput(tableno)
    document.all("FileInput"& tableno).outerHTML = ""
    NowFileInputCount = NowFileInputCount - 1
   End Sub
  </script>

<form id="form1" runat="server" enctype="multipart/form-data" method="post">

<TD style="WIDTH: 112px" align="right">产品图片:</TD>
       <TD align="left"><INPUT class="bk" οnclick="AddFileInput()" type="button" value="添加照片" name="add_line"></TD>

 Private Function Picdir() As String
        Dim dirname As String = System.DateTime.Now.Year.ToString() & System.DateTime.Now.Month.ToString()
        Dim dirpath = Server.MapPath("UploadImages/" & dirname)
        If Directory.Exists(dirpath) = False Then
            Directory.CreateDirectory(dirpath)
            Return dirpath & "/"
        Else
            Return dirpath & "/"
        End If
    End Function
    Private Function UpLoadPic() As String
        Dim filePath, picPath As String
        Dim i As Integer
        filePath = Picdir()
        picPath = ""
        For i = 0 To Request.Files.Count - 1
            Dim hpf As HttpPostedFile
            hpf = Request.Files(i)
            Dim fnx As Integer
            fnx = hpf.FileName.Length
            If fnx = 0 Then
                Return ""
            End If
            Dim fx As String = hpf.FileName.Substring(fnx - 4, 4).ToLower()
            If (fx <> ".jpg") And (fx <> ".gif") And (fx <> ".bmp") Then
                Response.Write("<javascript lanage='javascript'>alert('增加的:'" & hpf.FileName & "不是图片!')</script>")
                Return picPath
            End If
            Dim fn As String = System.DateTime.Now.ToString()
            fn = fn.Replace(":", "")
            fn = fn.Replace(" ", "")
            Dim picErr As String
            Try
                hpf.SaveAs(filePath & fn & i.ToString() & fx)
                picPath += filePath & fn & i.ToString() & fx & "#"
                picErr = "图片上传成功!"
            Catch ex As Exception
                Response.Write(ex.ToString)
                picErr = "图片上传失败!"
            End Try
        Next
        Return picPath
    End Function

  //=========================================================================================================================
    //检察目录
    private string productPicdir()
    {
        string dirname = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString();
        string dirpath = Server.MapPath("..//UploadedImages//product//" + dirname);
        if (!Directory.Exists(Server.MapPath("..//UploadedImages//product//" + dirname)))
        {
            Directory.CreateDirectory(Server.MapPath("..//UploadedImages//product//" + dirname));
            return dirpath + "//";
        }
        else
        {
            return dirpath + "//";
        }
    }
    //=========================================================================================================================
    //上传图片 返回图片路径
   private string upLoadPic()
   {
       string filePath = productPicdir();
       string picPath = "";
       for (int i =0; i <=(Request.Files.Count - 1); i++)
       {
         //使用Request.Files()来逐个获取上传的文件
         HttpPostedFile hpf;
         hpf = Request.Files[i];
         int fnx=hpf.FileName.Length;
         if (fnx == 0)
         {
             return "";
         }
         string fx = hpf.FileName.Substring(fnx - 4, 4).ToLower();
         if ((fx != ".jpg") && (fx != ".gif") && (fx != ".bmp"))
         {
             LTP.Common.MessageBox.Show(this, "增加的:"+hpf.FileName+"不是图片!");
             return picPath;
         }
         string fn = System.DateTime.Now.ToString();
         fn=fn.Replace(":", "");
         fn = fn.Replace(" ", "");
         try
         {
             hpf.SaveAs(filePath +fn+i.ToString()+fx);
             picPath += filePath + fn + i.ToString() + fx + "#";
             picErr = "图片上传成功!";
          }
         catch (Exception ex)
         {
              LTP.Common.MessageBox.Show(this, ex.ToString());
              picErr = "图片上传失败!";
         }
     
     }
       return picPath;
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值