电脑软硬件应用网
当前位置: 电脑软硬件应用网 > 设计学院 > 网络编程 > ASP教程 > 正文
ASP实现多图片上传到指定的目录并存到数据库
ASP实现多图片上传到指定的目录并存到数据库
2005-12-30 19:21:25  文/45IT收集   出处:电脑软硬件应用网   


关于图片上传的例子在网上有很多文章和原代码。但是每次上论坛浏览帖子的时候都是看到很多网友对图片上传感到头疼和麻烦。其实这个问题也是曾经让我感到头疼。也看过了不少的文章和代码。现在我写的这篇文章是把我比较喜欢的一篇代码和动网里的高手对这篇代码优化后再加上我增加一些代码结合出来的!呵呵,其实这篇文章是沾了写这篇代码的人(稻香居士)和动网里那些高手的光。:)
好了,不说废话了。开始来搭建所需要的环境和数据结构!
先新建一个名字叫photo的文件夹。(我在这里就是把图片上传到这个文件夹里的。)建立一个名字叫database的数据库。再接着建立一个名字叫sFile的表。表里设计四个字段分别是id(int),image(varchar),image1(varchar),image2(varchar)。我这里用的数据库是SQL。
相关的文件
register.asp
<html>
<head>
<title>文件</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<form name="form1" method="post" action="save.asp">
  <table width="50%" border="1" cellspacing="0" cellpadding="0" align="center">
    <tr> 
      <td width="20%"><a href="#"  OnClick="javascript:window.open('/reg_upload.asp',null,'    left=40%,top=40%,height=250,width=470,status=yes,toolbar=no,menubar=no,resizable=yes,copyhistory=yes,scrollbars=yes,location=no,status=no,titlebar=no')">图片上传</a></td>
      <td width="80%">   
        <input type="text" name="myface">
          
        <input type="text" name="myface1">
          
        <input type="text" name="myface2">
        (此处用户不必填图片上传后自动生成) </td>
    </tr>
    <tr> 
      <td colspan="2"  align="center"> 
        <input type="submit" name="Submit" value="提交">
        <input type="reset" name="Submit2" value="重设">
      </td>
    </tr>
  </table>
</form>
</body>
</html>

reg_upload.asp
<html>
<head>
<title></title>
<meta http-equiv="Content-Type"  content="text/html; charset=gb2312">
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body leftmargin="0" topmargin="0"   >
  <br>
<br>
<br>
<table width="90%" border="0" align="center" bgcolor="#000000" height="152" cellspacing="1">
  <tr> 
    <td height="34" align="center" bgcolor="#FFFFFF"><font color="#FFFF33"><b><font size="4" color="#000000">选择图片</font></b></font></td>
  </tr>
  <tr> 
    <td bgcolor="#FFFFFF"> 
      <form name="form"  method="post" action="upfile.asp" enctype="multipart/form-data" >
        <input type="hidden" name="filepath" value="photo">
        <input type="hidden"  name="act" value="upload">
        <input type="file" name="file1"  size="30"><br>
         <input type="file" name="file2" size="30"><br>
         <input type="file" name="file3" size="30">
           
        <input  type="submit" name="Submit" value="粘 贴" class="tl">
      </form>
    </td>
  </tr>
</table>
</body>
</html>

upfile.asp
<!--#include FILE="upload.inc"-->
<html>
<head>
<title>文件上传</title>
</head>
<body>
<%
dim arr(3)
dim upload,file,formName,formPath,iCount,filename,fileExt,i
set upload=new upload_5xSoft ''建立上传对象


formPath=upload.form("filepath")
''在目录后加(/)
if right(formPath,1)<>"/" then formPath=formPath&"/" 
iCount=0
i=0
for each formName in upload.file ''列出所有上传了的文件
    set file=upload.file(formName)  ''生成一个文件对象
   
    if file.filesize<0 then
        response.write "<font size=2>请先选择你要上传的图片 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</font>"
        response.end
    end if
   
    if file.filesize>100000 then
        response.write "<font size=2>图片大小超过了限制 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</font>"
        response.end
    end if
if fileEXT<>".gif" and fileEXT<>".jpg" then
     response.write "<font size=2>文件格式不对 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</font>"
    response.end
end if 

    fileExt=lcase(right(file.filename,4))
    filename=formPath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&file.FileName
    arr(i)=filename
    i=i+1

    if file.FileSize>0 then         ''如果 FileSize > 0 说明有文件数据
        file.SaveAs Server.mappath(filename)   ''保存文件
        ' response.write file.FilePath&file.FileName&" ("&file.FileSize&") => "&formPath&File.FileName&" 成功!<br>"

          iCount=iCount+1
    end if
    set file=nothing
next
if instr(arr(0),".")=0 and instr(arr(1),".")=0  and instr(arr(2),".")=0  then
  response.write "<font size=2>请先选择你要上传的图片 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</font>"
response.end
end if
if instr(arr(0),".")<>0 then
response.write "<script>opener.document.forms[0].myface.value='" & arr(0) & "'</script>"
else
response.write "<script>opener.document.forms[0].myface.value=''</script>"
end if
if instr(arr(1),".")<>0 then
response.write "<script>opener.document.forms[0].myface1.value='" & arr(1) & "'</script>"
else
response.write "<script>opener.document.forms[0].myface1.value=''</script>"
end if
if instr(arr(2),".")<>0 then
response.write "<script>opener.document.forms[0].myface2.value='" & arr(2) & "'</script>"
else
response.write "<script>opener.document.forms[0].myface2.value='' </script>"
end if
set upload=nothing  ''删除此对象

session("upface")="done"

Htmend iCount&" 个文件上传结束!"

sub HtmEnd(Msg)
    set upload=nothing
response.write "<html><head><meta  http-equiv='Refresh' content='3 url

[1] [2] [3] 下一页

  • 上一篇文章:

  • 下一篇文章:
  • 最新热点 最新推荐 相关文章
    三种中文分词算法优劣比较
    ASP教程:删除记录和链接数据库程序解…
    ASP生成html或者txt文件实例
    分享常用7款天气预报代码iframe嵌入…
    ASP错误信息解决:IIS启用父路径的设…
    禁止网页缓存的方法及代码
    推荐如何抓取显示防盗链图片的方法
    为什么浏览不了dreamweaver中ASP文件…
    asp入门教程之讲解ASP脚本执行的顺序
    asp将access数据导出为excel电子表的…
    关于45IT | About 45IT | 联系方式 | 版权声明 | 网站导航 |

    Copyright © 2003-2011 45IT. All Rights Reserved 浙ICP备09049068号