[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。
发表人 主题:我想简化你的代码,可不成功。
王新安
身份:user
发帖:60
登陆次数:23
1F 发表于 2006/7/31 21:17:27
版主,你的上传图片代码是不是出自稻香老家的代码啊?那个代码本来就不懂,我都是直接拿过来用。看到你的上传页面,比它的漂亮多了,自己也心动了,经过一番折服,其中去掉那个上传到哪个文件的文本框和那个函数里的class要改名称,费了我好大劲才发现。
现在看到你的既上传图片,又能显示图片。又心动了。
看你的代码,那么多条件,看得头都大了。想简化下你的代码,就是上传图片,当然判断下是不是图片,然后图片属性的数据插入数据库。却不成功。版主看看这段代码为何不成功。


<!--#include File="conn.asp" -->
<!--#include File="upload_5xsoft.inc"-->
<%dim upload,file,formName,formPath,iCount,fileExt,ranNum,filename,myIndex
set upload=new upload_5xsoft ''''建立上传对象
formPath="upfile/" ''保存位置
strRestrictBy = "ContentType"  ''or extension  文件验证方式
iCount=0
%>

<% 
for each formName in upload.objFile ''''列出所有上传了的文件
     set file=upload.file(formName)  ''''生成一个文件对象
     if file.FileSize>0 then         ''''如果 FileSize > 0 说明有文件数据
        fileOK=1
        fileExt = LCase(Mid(file.Filename, InStrRev(file.Filename, ".")))  ''取得扩展名
        ''===================检查文件大小=====
        if file.FileSize>upFileSize then
            errMsg=errMsg+"文件:"&file.Filename&" 大于"&upFileSize\1024&"KB!<br>"
            fileOK=0
        else
            If strRestrictBy = "extension" Then  ''--验证方式为扩展名
                ''===================检查后缀名=====
                Select Case LCase(fileExt)
                    Case ".jpg", ".gif", ".bmp",".png",".tiff",".swf"
                    Case Else
                        fileOK=0
                        Response.Write("<B>错误:</B> 扩展名为 <I>" & fileExt & "</I> 的文件不能被上传。<BR>")
                End Select
            Else ''--验证方式为MIME类型
                strContentType =file.FileType
                Select Case LCase(strContentType)
                    Case "image/gif", "image/jpeg", "image/pjpeg","image/bmp","image/tiff","image/x-png","image/png","application/x-shockwave-flash"
                    Case Else
                        fileOK=0
                        Response.Write("<B>错误:</B> MIME类型为 <I>" & strContentType & "</I> 的文件不能被上传。<BR>")
                End Select
            End If ''--end if 验证方式
        end if
        
        if fileOK=1 then ''--如果通过检查,那么保存文件
            randomize
            ranNum=int(900*rnd)+100
            filename=year(now())&month(now())&day(now())&hour(now())&minute(now())&second(now())&ranNum&fileExt
            file.SaveAs Server.mappath(formPath&filename)  ''''保存文件
            ''=========添加文件的信息到数据库里,默认没有锁定=====
            myIndex=right(formName,1)
            temp_photoTitle=upload.form("photoTitle"+myIndex)
            temp_photoIntro=upload.form("photoIntro"+myIndex)
            temp_photoWidth=upload.form("photoWidth"+myIndex)
            temp_photoHeight=upload.form("photoHeight"+myIndex)
            ''========检查输入,为空则给初值===
            temp_photoTitle=replace(trim(temp_photoTitle),"''","''''")
            if temp_photoTitle="" then
                temp_photoTitle="没有填写"
            end if
            temp_photoIntro=replace(trim(temp_photoIntro),"''","''''")
            if temp_photoIntro="" then
                temp_photoIntro="没有填写"
            end if
            if temp_photoWidth="" or not IsNumeric(temp_photoWidth) then
                temp_photoWidth=160
            end if
            if temp_photoHeight="" or not IsNumeric(temp_photoHeight) then
                temp_photoHeight=120
            end if
            ''=======插入数据库===
            sql="insert into TBL_PHOTO(addTime,photoFilename,photoTitle,photoIntro,photoClick,photoSize,photoWidth,photoHeight) values(''"&Now()&"'',''"&filename&"'',''"&temp_photoTitle&"'',''"&temp_photoIntro&"'',1,"&file.FileSize&","&temp_photoWidth&","&temp_photoHeight&")"
            conn.execute sql
                    
            ''======输出上传成功信息===
            response.write file.FilePath&file.FileName&" ("&file.FileSize&") => "&filename&" 成功!<br>"
            iCount=iCount+1
        end if
     end if
     set file=nothing
next

set upload=nothing  ''''删除此对象
response.write "<br>"&iCount&" 个文件上传结束!"
response.write "<br><a href=''photo_listphoto.asp''><B>返回相册</B></a>"
''=============如果有错,输出错误信息=============================    
if errMsg<>"" then
    response.write "<br>"&errMsg
    response.write "<INPUT type=''button'' onClick=''history.go(-1)'' value=''返回'' class=''myInput''>"
end if
conn.close
set conn=nothing
%>



王新安
身份:user
发帖:60
登陆次数:23
2F 发表于 2006/7/31 21:20:14
下面的是我的上传页面,就是稻香老农稍改下如你的样子的。


<%if request.cookies("adminok")="" then
response.redirect "login.html"
end if%>
<html>
<head>
<title>化境上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
td {  font-size: 9pt}
a {  color: #000000; text-decoration: none}
a:hover {  text-decoration: underline}
.tx {
    color: #990000;
    background-color: #FFEBE6;
    border: 1px solid #990000;
}
.bt {
    background-color: #FFDFDF;
    font-size: 12px;
    border-top: 1px outset #FFE8E8;
    border-right: 1px outset #990000;
    border-bottom: 1px outset #990000;
    border-left: 1px outset #FFE8E8;
    vertical-align: bottom;
    height: 20px;
}
-->
</style>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="addfile.asp" enctype="multipart/form-data" >
 
  <br>
  <input type="hidden" name="act" value="upload">
  <br>
  <table width="71%" border="0" cellspacing="0" cellpadding="5" align="center" style="border:1px solid #990000;">
    <tr> 
      <td height="22" align="left" valign="middle" bgcolor="#FFDFDF"> 图片上传</td>
    </tr>
    <tr align="left" valign="middle"> 
      <td  height="92"> 
        <script language="javascript">
      function setid()
      {
      str=''<br>'';
      if(!window.form1.upcount.value)
       window.form1.upcount.value=1;
       for(i=1;i<=window.form1.upcount.value;i++)
         str+=''文件''+i+'':<input type="file" name="file''+i+''" style="width:400" class="tx"><br><br>'';
      window.upid.innerHTML=str+''<br>'';
      }
      </script>
        <li> 上传图片的数目 
          <input type="text" name="upcount" class="tx" value="1">
          <input type="button" name="Button" class="bt" onclick="setid();" value="· 设定 ·">
        </li>
        

      </td>
      <tr><td cols=2 bgcolor=#ffdfdf>选择你要上传的图片</td></tr>
    </tr>
    <tr align="center" valign="middle"> 
      <td align="left" id="upid" height="122"> 文件1: 
        <input type="file" name="file1" style="width:400" class="tx" value="">
      </td>
    </tr>
    <tr align="center" valign="middle"> 
      <td height="24"> 
        <input type="submit" name="Submit" value="· 提交 ·" class="bt">
        <input type="reset" name="Submit2" value="· 重执 ·" class="bt">
      </td>
    </tr>
  </table>
</form>
</body>
</html>
<script language="javascript">

setid();
</script>



王新安
身份:user
发帖:60
登陆次数:23
3F 发表于 2006/7/31 21:43:01
那是上传后错误的截图:

按此在新窗口浏览图片

嘎嘎,是我
身份:admin
发帖:1435
登陆次数:3222
4F 发表于 2006/8/1 9:27:00
1。upFileSize 这个变量你得给个值,比如upFileSize=1024*1024

2。你得上传页面只有一个文件,其他的宽度,高度,标题都没有,所以你应该把这些地方都取去掉。
=========添加文件的信息到数据库里,默认没有锁定=====
.......这个位置的代码都没用。
=======插入数据库===
3。然后你的插入SQL就得改改了。

王新安
身份:user
发帖:60
登陆次数:23
5F 发表于 2006/8/2 13:12:14
版主,照你说的做,成功了。
于是想把宽度,高度,标题都加上。图片是上传成功,但却插入不了数据!
这是第一个文件的代码


<style>
.input-disabled {
    background-color: #F4CECE;
    border: 1px dashed #000000;

}
</style>
<form name="fileForm" method="post" action="addfile2.asp" enctype="multipart/form-data">

   上传说明:最多可以同时上传五个文件,其中标题最多30字,简介200字。
 <table width="96%" align="center" cellpadding="4" cellspacing="2">
    <tr align="center" valign="middle">
      <td align="left" bgcolor="#F4CECE" id="upid">文件1</td> 
      <td height="29" align="left" bgcolor="#F4CECE" id="upid">            <INPUT name="file1" type="file" class="myInput" size="20">
        宽:
          <INPUT name="photoWidth1" type="text" class="input-disabled" id="photoWidth1" size="5" readonly="">
          高:
          <INPUT name="photoHeight1" type="text" class="input-disabled" id="photoHeight1" size="5" readonly="">
          大小:
          <INPUT name="fileSize1" type="text" class="input-disabled" id="fileSize1" size="5" readonly="">          
      </td>
    </tr>
    <tr align="center" valign="middle">
      <td colspan="2" align="left" bgcolor="#FDF2F2" id="upid">
        标题:
          <INPUT name="photoTitle1" type="text" class="myInput" id="photoTitle1" size="40">
          <BR>        
        简介:
        <TEXTAREA name="photoIntro1" cols="60" rows="5" class="myInput" id="photoIntro1"></TEXTAREA>
      </td>
      </tr>
    <tr align="center" valign="middle">
      <td align="left" bgcolor="#F4CECE" id="upid">文件2</td>
      <td height="29" align="left" bgcolor="#F4CECE" id="upid">
        <INPUT name="file2" type="file" class="myInput" id="file2" size="20" >
    宽:
    <INPUT name="photoWidth2" type="text" class="input-disabled" id="photoWidth2" size="5" readonly="">
    高:
    <INPUT name="photoHeight2" type="text" class="input-disabled" id="photoHeight2" size="5" readonly="">
    大小:
    <INPUT name="fileSize2" type="text" class="input-disabled" id="fileSize2" size="5" readonly="">
      </td>
    </tr>
    <tr align="center" valign="middle">
      <td colspan="2" align="left" bgcolor="#FDF2F2" id="upid">
        标题:
          <INPUT name="photoTitle2" type="text" class="myInput" id="photoTitle2" size="40">
          <BR>
简介:
<TEXTAREA name="photoIntro2" cols="60" rows="5" class="myInput" id="photoIntro2"></TEXTAREA>
      </td>
      </tr>
    <tr align="center" valign="middle">
      <td align="left" bgcolor="#F4CECE" id="upid">文件3</td>
      <td height="29" align="left" bgcolor="#F4CECE" id="upid">
        <INPUT name="file3" type="file" class="myInput" id="file3" size="20" >
    宽:
    <INPUT name="photoWidth3" type="text" class="input-disabled" id="photoWidth3" size="5" readonly="">
    高:
    <INPUT name="photoHeight3" type="text" class="input-disabled" id="photoHeight3" size="5" readonly="">
    大小:
    <INPUT name="fileSize3" type="text" class="input-disabled" id="fileSize3" size="5" readonly="">
      </td>
    </tr>
    <tr align="center" valign="middle">
      <td colspan="2" align="left" bgcolor="#FDF2F2" id="upid">
        标题:
          <INPUT name="photoTitle3" type="text" class="myInput" id="photoTitle3" size="40">
          <BR>
简介:
<TEXTAREA name="photoIntro3" cols="60" rows="5" class="myInput" id="photoIntro3"></TEXTAREA>
      </td>
      </tr>
    <tr align="center" valign="middle">
      <td align="left" bgcolor="#F4CECE" id="upid">文件4</td>
      <td height="29" align="left" bgcolor="#F4CECE" id="upid">
        <INPUT name="file4" type="file" class="myInput" id="file4" size="20" >
    宽:<INPUT name="photoWidth4" type="text" class="input-disabled" id="photoWidth4" size="5" readonly="">
    高: <INPUT name="photoHeight4" type="text" class="input-disabled" id="photoHeight4" size="5" readonly="">
    大小:<INPUT name="fileSize4" type="text" class="input-disabled" id="fileSize4" size="5" readonly="">
      </td>
    </tr>
    <tr align="center" valign="middle" bgcolor="#FDF2F2">
      <td colspan="2" align="left" id="upid">
        标题:
          <INPUT name="photoTitle4" type="text" class="myInput" id="photoTitle23" size="40">
          <BR>
简介:
<TEXTAREA name="photoIntro4" cols="60" rows="5" class="myInput" id="photoIntro4"></TEXTAREA>
      </td>
      </tr>
    <tr align="center" valign="middle">
      <td align="left" bgcolor="#F4CECE" id="upid">文件5</td>
      <td height="27" align="left" bgcolor="#F4CECE" id="upid">
        <INPUT name="file5" type="file" class="myInput" id="file5" size="20" >
    宽:
    <INPUT name="photoWidth5" type="text" class="input-disabled" id="photoWidth5" size="5" readonly="">
    高:
    <INPUT name="photoHeight5" type="text" class="input-disabled" id="photoHeight5" size="5" readonly="">
    大小:
    <INPUT name="fileSize5" type="text" class="input-disabled" id="fileSize5" size="5" readonly="">
      </td>
    </tr>
    <tr align="center" valign="middle">
      <td colspan="2" align="left" bgcolor="#FDF2F2" id="upid">
        标题:
          <INPUT name="photoTitle5" type="text" class="myInput" id="photoTitle24" size="40">
          <BR>
简介:
<TEXTAREA name="photoIntro5" cols="60" rows="5" class="myInput" id="photoIntro5"></TEXTAREA>
      </td>
      </tr>
    <tr align="center" valign="middle" bgcolor="#F4CECE">
      <td height="24" colspan="2"> 
        <input name="Submit" type="submit" class="myButton" value="开始上传">
        <input name="Submit2" type="reset" class="myButton" value="重新填写">
      </td> 
      </tr>
  </table>
</form>


这是第二个文件的代码


<!--#include File="conn.asp" -->
<!--#include File="upload_5xsoft.inc"-->
<%dim upload,file,formName,formPath,iCount,fileExt,ranNum,filename,myIndex
set upload=new upload_5xsoft ''''''''建立上传对象
formPath="upfile/" ''''保存位置
strRestrictBy = "ContentType"  ''''or extension  文件验证方式
iCount=0
%>

<% 
for each formName in upload.objFile ''''''''列出所有上传了的文件
     set file=upload.file(formName)  ''''''''生成一个文件对象
     if file.FileSize>0 then         ''''''''如果 FileSize > 0 说明有文件数据
        fileOK=1
        fileExt = LCase(Mid(file.Filename, InStrRev(file.Filename, ".")))  ''''取得扩展名
        ''''===================检查文件大小=====
        Dim upFileSize
        upFileSize=1024*1024
        if file.FileSize>upFileSize then
            errMsg=errMsg+"文件:"&file.Filename&" 大于"&upFileSize\1024&"KB!<br>"
            fileOK=0
        else
            If strRestrictBy = "extension" Then  ''''--验证方式为扩展名
                ''''===================检查后缀名=====
                Select Case LCase(fileExt)
                    Case ".jpg", ".gif", ".bmp",".png",".tiff",".swf"
                    Case Else
                        fileOK=0
                        Response.Write("<B>错误:</B> 扩展名为 <I>" & fileExt & "</I> 的文件不能被上传。<BR>")
                End Select
            Else ''''--验证方式为MIME类型
                strContentType =file.FileType
                Select Case LCase(strContentType)
                    Case "image/gif", "image/jpeg", "image/pjpeg","image/bmp","image/tiff","image/x-png","image/png","application/x-shockwave-flash"
                    Case Else
                        fileOK=0
                        Response.Write("<B>错误:</B> MIME类型为 <I>" & strContentType & "</I> 的文件不能被上传。<BR>")
                End Select
            End If ''''--end if 验证方式
        end if
        
        if fileOK=1 then ''''--如果通过检查,那么保存文件
            randomize
            ranNum=int(900*rnd)+100
            filename=year(now())&month(now())&day(now())&hour(now())&minute(now())&second(now())&ranNum&fileExt
            file.SaveAs Server.mappath(formPath&filename)  ''''''''保存文件
            ''''=========添加文件的信息到数据库里,默认没有锁定=====
            myIndex=right(formName,1)
            temp_photoTitle=upload.form("photoTitle"+myIndex)
            temp_photoIntro=upload.form("photoIntro"+myIndex)
            temp_photoWidth=upload.form("photoWidth"+myIndex)
            temp_photoHeight=upload.form("photoHeight"+myIndex)
            ''''========检查输入,为空则给初值===
            temp_photoTitle=replace(trim(temp_photoTitle),"''''","''''''''")
            if temp_photoTitle="" then
                temp_photoTitle="没有填写"
            end if
            temp_photoIntro=replace(trim(temp_photoIntro),"''''","''''''''")
            if temp_photoIntro="" then
                temp_photoIntro="没有填写"
            end if
            if temp_photoWidth="" or not IsNumeric(temp_photoWidth) then
                temp_photoWidth=160
            end if
            if temp_photoHeight="" or not IsNumeric(temp_photoHeight) then
                temp_photoHeight=120
            end if
            ''''=======插入数据库===
            sql="insert into TBL_PHOTO(addTime,photoFilename,photoTitle,photoIntro,photoClick,photoSize,photoWidth,photoHeight) values(''''"&Now()&"'''',''''"&filename&"'''',''''"&temp_photoTitle&"'''',''''"&temp_photoIntro&"'''',1,"&file.FileSize&","&temp_photoWidth&","&temp_photoHeight&")"
            conn.execute sql
                    
            ''''======输出上传成功信息===
            response.write file.FilePath&file.FileName&" ("&file.FileSize&") => "&filename&" 成功!<br>"
            iCount=iCount+1
        end if
     end if
     set file=nothing
next

set upload=nothing  ''''''''删除此对象
response.write "<br>"&iCount&" 个文件上传结束!"
response.write "<br><a href=''''photo_listphoto.asp''''><B>返回相册</B></a>"
''''=============如果有错,输出错误信息=============================    
if errMsg<>"" then
    response.write "<br>"&errMsg
    response.write "<INPUT type=''''button'''' onClick=''''history.go(-1)'''' value=''''返回'''' class=''''myInput''''>"
end if
conn.close
set conn=nothing
%>


望版主再次指点迷津!

王新安
身份:user
发帖:60
登陆次数:23
6F 发表于 2006/8/2 13:13:55
你的高度,宽度的值是哪里来的!

王新安
身份:user
发帖:60
登陆次数:23
7F 发表于 2006/8/2 13:15:31
我现在看你的那些代码很困难啊。55555~~~~

嘎嘎,是我
身份:admin
发帖:1435
登陆次数:3222
8F 发表于 2006/8/2 19:42:56
我那个调用JS来取得高度和宽度的。
干脆就照抄我那个算了……哈哈,反正写到最后都差不多的。

王新安
身份:user
发帖:60
登陆次数:23
9F 发表于 2006/8/2 23:04:01
唉,照抄算了。嘻嘻~~

 
标题:
消息图标:                                             
                                            
正文:



* UBB 代码开启

 
CopyRight © 心缘地方 2005-2999. All Rights Reserved