回 帖 发 新 帖 刷新版面

主题:[原创]批量上传产品图片,但不能在数据库中写入图片名称

从前一个网页传递过来style_no,id 两个参数。在以下页面接受参数并根据它们,将批量上传的图片名称写入数据库的imgbig表中,同时在产品表product中改大图的标志为1.但是,现在如果我只上传一个图片的话,图片名称就可以写入imgbig表,如果上传多个图片,就只能写入最后一个图片的名称,或者有时候都不能写入图片的名称(图片上传到指定的文件夹都是正常的)。不知道我哪里的语句还要改写?请各位前辈看看。谢谢啦。

...
上传类
...

<HTML>
<HEAD>
<STYLE TYPE="text/css">
<!--
.p9{ font-size: 9pt; font-family: 宋体 }
td {font-size: 9pt}
.tx {height: 16px; width: 30px; border-color: black black #000000; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; font-size: 9pt; background-color: #eeeeee; color: #0000FF}
.tx2 {height: 16px;border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; font-size: 9pt; color: #0000FF; border-left-color:#000000; border-right-color:#000000; border-top-color:#000000; border-bottom-color:#000000}
.bt {border-left:1px solid #C0C0C0; border-top:1px solid #C0C0C0; font-size: 9pt; border-right-width: 1; border-bottom-width: 1; height: 16px; width: 80px; background-color: #EEEEEE; cursor: hand; border-right-style:solid; border-bottom-style:solid}
.tx1 { width: 400 ;height: 20px; font-size: 9pt; border: 1px solid; border-color: black black #000000; color: #0000FF}
-->
</STYLE>
<TITLE>上传文件</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
<SCRIPT LANGUAGE="vbscript">
function addfile()
dim str
str="<table>"
if not IsNumeric (window.form1.filenum.value) then window.form1.filenum.value =1
for i=1 to window.form1.filenum.value
str=str&"<tr><td valign='middle'>文件"&i&":</td><td><input type='file' name='file"&i&"' class='tx1' value size='20'>  保存为<input type='text' name='file"&i&"' size='20' class='tx2'></td></tr>"
next
window.uptd.innerHTML =str&"</table>"
end function
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#ffffff" CLASS="p9" onLoad="addfile()">
<%
dim upfile,formPath,ServerPath,FSPath,formName,FileName,oFile,upfilecount
upfilecount=0
set upfile=new clsUp ''建立上传对象
upfile.NoAllowExt="code;" '设置上传类型的黑名单
upfile.GetData (10240000)   '取得上传数据,限制最大上传10M
if upfile.isErr then '如果出错
    select case upfile.isErr
case 1
case 2
Response.Write "你上传的文件超出我们的限制,最大10M"
end select
else
%>
<table align="center" width="750" border="1" cellpadding="0" cellspacing="0" bordercolor="#000000" class="p9" style="border-collapse: collapse">
<tr bgcolor="#CCCCCC"> 
    <td height="25" valign='middle'> 本地文件 </td>
    <td valign='middle'> 大小(字节) </td>
    <td valign='middle'> 上传到 </td>
    <td valign='middle'> 状态 </td>
</tr> 
<%   
   dim sql3,rs3,style_no
   style_no=trim(Request.QueryString("style_no"))
   sql3="select * from bigimg where style_no='"&style_no&"'"
   set rs3=server.createobject("adodb.recordset")  
   rs3.open sql3,conn,1,1
   if  rs3.eof then 
       Set cmdTemp = Server.CreateObject("ADODB.Command")
       Set InsertCursor = Server.CreateObject("ADODB.Recordset")
       cmdTemp.CommandText = "SELECT top 1 * FROM bigimg order by id desc"
       cmdTemp.CommandType = 1
       Set cmdTemp.ActiveConnection = conn
       InsertCursor.Open cmdTemp, , 1, 3
       InsertCursor.addnew 
       InsertCursor("style_no") =style_no    
       InsertCursor.Update
       InsertCursor.close
       set InsertCursor=nothing
   else 
   FSPath=GetFilePath(Server.mappath("uptofile.asp"),"\")                   '取得当前文件在服务器路径
   ServerPath=GetFilePath(Request.ServerVariables("HTTP_REFERER"),"/")      '取得在网站上的位置
   for each formName in upfile.file               '列出所有上传了的文件
      set oFile=upfile.file(formname)
      FileName=upfile.form(formName)              '取得文本域的值
      if not FileName<>"" then  FileName=oFile.filename  '单行if。如果没有输入新的文件名,就用原来的文件名
      dim sql,i
      for i=2 to rs3.fields.count             'rs3(0)是id字段,rs3(1)是style_no字段,所以要从rs3(2)
         if rs3(i)="nothing" then     '当字段的值为nothing,即是该字段还没有存放图片文件名称,即可以存放图片
             sql="update bigimg set "&rs3(i).name&"='"&FileName&"' where style_no='"&style_no&"'"
             conn.Execute (sql)        'rs3(i)是字段里面记录的值,rs3(i).name是字段的名称
             exit for
         end if
      next
   upfile.SaveToFile formname,FSPath&FileName     ' 保存文件(图片)     也可以使用AutoSave来保存,参数一样,但是会自动建立新的文件名
   %>
<tr> 
    <td height="20" valign='middle'> <%=oFile.FilePath&oFile.FileName%> </td>
    <td valign='middle'> <%=INT((oFile.filesize)/1024)%>KB </td>
    <td valign='middle'> <A HREF="<%=serverpath&FileName%>"><%=FileName%></A> </td>
    <td valign='middle'> 
     <%
      if upfile.iserr then 
          Response.Write upfile.errmessage
      else
         upfilecount=upfilecount+1
         Response.Write "上传成功"
      end if
     %>
    </td>
  </tr>
  <%
     set oFile=nothing
   next
   end if

dim sql2,rs2,sql4,imgbig4
sql2="select bigImg from product where id="&request.QueryString("id")
set rs2=conn.execute (sql2)
imgbig4="1"                    '对product表中的大图字段做标识,为1就是由大图,为nothing就是没有大图
if rs2("bigImg")="nothing" then
  sql4="update product set bigImg='"&imgbig4&"' where id="&request.QueryString("id")
  conn.Execute (sql4)
end if
%>
<tr> 
    <td colspan="3" height="25" valign='middle'> 一共上传了<%=upfileCount%>个文件</td>
</tr>
<%
end if
set upfile=nothing '删除此对象
%>
</p>
</table>
<FORM METHOD="post" NAME="form1" ENCTYPE="multipart/form-data">
<TABLE BORDER="0" ALIGN="center" CELLPADDING="0" CELLSPACING="0">
    <TR> 
      <TD><TABLE WIDTH="750" BORDER="1" ALIGN="center" CELLPADDING="0" CELLSPACING="0" BORDERCOLOR="#111111" STYLE="BORDER-COLLAPSE: collapse">
          <TR> 
            <TD HEIGHT="27" COLSPAN="2">上传<INPUT NAME="filenum" CLASS="tx2" VALUE="1" SIZE="4">个文件   
              <INPUT TYPE="button" NAME="Button" CLASS="bt" onClick="addfile" VALUE="设 定">
            </TD>
          </TR>
          <TR> 
            <TD> <DIV ID="uptd"> </DIV></TD>
          </TR>
          <TR> 
            <TD HEIGHT="30" COLSPAN="2" ALIGN="middle"> 
              <INPUT TYPE="submit" NAME="Button" CLASS="bt" VALUE="上 传">
              <INPUT TYPE="reset" NAME="Button" CLASS="bt" VALUE="重 置">
            </TD>
          </TR>
        </TABLE></TD>
    </TR>
</TABLE>
</FORM>

</BODY>
</HTML>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JS上传前预览图片</title>
<script type="text/javascript" language="javascript">
<!--
function PreviewImg(imgFile){
  
    var newPreview = document.getElementById("newPreview");    
    var imgDiv = document.createElement("div");
    document.body.appendChild(imgDiv);
    imgDiv.style.width = "158px";    imgDiv.style.height = "127px";
    imgDiv.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod = scale)";   
    imgDiv.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile.value;
    newPreview.appendChild(imgDiv);    
    var showPicUrl = document.getElementById("showPicUrl");
    showPicUrl.innerText=imgFile.value;
    newPreview.style.width = "80px";
    newPreview.style.height = "60px";
}
-->
</script>
    </head>
    <body style="text-align:center;"> 
        <div style="text-align:center;width:750px;">   
<fieldset id="newPreview">
<legend align="center">图片预览</legend>
        <div id="newPreview"></div>
        <div id="showPicUrl"></div></fieldset>
        <hr />
        <p>
            选择图片:<input type="file" size="20" onChange="javascript:PreviewImg(this);" />
        </p>
        </div>
    </body>
</html>
<%
       conn.close
       set conn=nothing
%>


当然,如果哪位前辈有批量上传图片(并在数据库中写入名称)的ASP源代码,也请提供一份,我的邮箱是hongutay@163.com。谢谢了。

回复列表 (共2个回复)

沙发

有必要自己去研究上传代码吗?自己直接将别人的拿过来使用就行了
见这个:http://www.aspprogram.cn/html/soft/2008624191142.html
会用就行了

板凳

1楼的朋友:
你提供那个上传文件没有符合我的实际要求的。不过还是谢谢啦。

我来回复

您尚未登录,请登录后再回复。点此登录或注册