ASP无组件同时上传图片和文字

商帝国咨询头像

阅读 5886 | 分类:文章

   昨日花了半天时间,最终帮一下属单位解决了ASP无组件同时上传图片和文字的问题。系统结构采用IIS+ASP+SQL SERVER,其中一小部分功能需求主要是在同一个页面同时上传1至3张图片,保存至服务器指定目录;同时将图片标题、内容说明、发送人、发送时间等字段保存到SQL SERVER数据库,一键完成上传;实现上传图片、文字的网页调用。
    原系统采用的是LYF的免费组件,非常好用,其他多个部门安装使用均无问题,唯独昨日这家组件注册后上传文字均为乱码,图片不能上传,再考虑有的服务器不允许安装组件的情况,特别改用了这个方案。编写过程中,网上搜索了不少相关的经验文章,发现虽然此类方法、例子较多,但能满足上述功能的现成源码多不理想,特别是图片、文字同时上传方面不能满足使用,后参考风声无组件上传类作了修改,基本完善解决。

关键源码:
1、上传页
Head部分(用于控制当前上传图片数量选择)
<script language="JavaScript">
<!--
function setFileFileds(num){ 
 for(var i=0,str="";i<num;i++){
  str+="<input name=\"strFile"+i+"\" type=\"file\" id=\"strFile"+i+"\"><br>";
 }
 objFiles.innerHTML=str;
}
//-->
</script>

表单部分
<form method="POST" enctype="multipart/form-data" action="photosbxxok.asp" name="form1" >
照片标题: <input type="text" name="title" size="66">
文字说明: <textarea name="content" rows="8"></textarea>
<td valign="middle" align="center" height="30" width="20%">图片数量:
 <select name="select" onChange="setFileFileds(this.value)">
   <option value="1" selected>1</option>
   <option value="2">2</option>
   <option value="3">3</option>
   </select>
</td>
<td id="objFiles" valign="top" align="left" height="30" width="80%" ></td>
<script language="JavaScript">setFileFileds(form1.select.value)</script>

2、上传处理文件(photosbxxok.asp)
<!-- #i nclude file="common.inc" -->'数据库连接文件

<!--#i nclude FILE="UpLoadClass.asp"-->
<%
dim request2,formPath,formName,intCount,intTemp
'建立上传对象
set request2=new UpLoadClass
 '设置文件允许的附件类型为gif/jpg/rar/zip
 request2.FileType="gif/jpg/rar/zip"
 '设置服务器文件保存路径
 request2.SavePath="gaxxphoto/"
 '打开对象
 request2.open() 
%>
<html>
<head>
<title>照片已成功上报</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="/basic.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="760" border="0" align="center" cellpadding="5" cellspacing="0" bgcolor="#FFFFFF" style="border:1px solid #999999">
  <tr> 
    <td><blockquote>
 <%
 '显示标题
 response.Write("<br>标题:"&request2.Form("Title"))
 '显示内容
 response.Write("<br>内容:"&request2.Form("Content"))
'打开数据库中表main
set rs = Server.CreateObject("ADODB.RecordSet")
rs.open "main",conn,1,3
'保存文字到数据库
rs.addnew
rs("title")=request2.Form("title")
rs("content")=request2.Form("content")
 '----列出所有上传了的文件开始----
 intCount=0
 for intTemp=1 to Ubound(request2.FileItem)
  '获取表单文件控件名称,注意FileItem下标从1开始
  formName=request2.FileItem(intTemp)
  '显示文件域
  response.write "<br>"&intTemp&"、文件域["&formName&"]:"
  '显示源文件路径与文件名
  response.write "<br>"&request2.form(formName&"_Path")&request2.form(formName&"_Name")
  '显示文件大小(字节数)
  response.write "("&request2.form(formName&"_Size")&") => "
  '显示目标文件路径与文件名
  response.Write formPath&request2.form(formName)&" "
  '显示文件保存状态
  select case request2.form(formName&"_Err")
   case -1:
    response.write "没有文件上传<br>"
   case 0:
    response.write "上传文件成功<br>"
    intCount=intCount+1
   case 1:
    response.write "文件太大,拒绝上传<br>"
   case 2:
    response.write "文件格式不对,拒绝上传<br>"
   case 3:
    response.write "文件太大且格式不对,拒绝上传<br>"
  end select

photoname=photoname&request2.Form(formName)
 next
'将上传自动更名后文件名存入数据库
rs("photo")=photoname
rs.update
rs.close
set rs=nothing
'----列出所有上传了的文件结束---- 
 response.write "<br>"&intCount&" 个文件上传成功! "
 response.write "[<a href=""javascript:history.back();"">返回</a>]"
 %>
 </blockquote></td>
  </tr>
</table>
</body>
</html>
<%
'释放上传对象
set request2=nothing
%>


3、调用(三张)
<%if len(photo)=20 then%>
 <a href="/2006/gaxxphoto/<%=photo%>"><img src="/2006/gaxxphoto/<%=photo%>" width="150" height="110" border=0></a>
<%end if%>

<%if len(photo)=40 then%>
<a href="/2006/gaxxphoto/<%=left(photo,20)%>"><img src="/2006/gaxxphoto/<%=left(photo,20)%>" width="150" height="110" border=0></a>
<a href="/2006/gaxxphoto/<%=right(photo,20)%>"><img src="/2006/gaxxphoto/<%=right(photo,20)%>" width="150" height="110" border=0></a>
<%end if%>

<%if len(photo)=60 then%>
<a href="/2006/gaxxphoto/<%=left(photo,20)%>"><img src="/2006/gaxxphoto/<%=left(photo,20)%>" width="150" height="110" border=0></a>
<a href="/2006/gaxxphoto/<%=mid(photo,21,20)%>"><img src="/2006/gaxxphoto/<%=mid(photo,21,20)%>" width="150" height="110" border=0></a>
<a href="/2006/gaxxphoto/<%=right(photo,20)%>"><img src="/2006/gaxxphoto/<%=right(photo,20)%>" width="150" height="110" border=0></a>
<%end if%>

留言区(共0条)

暂无留言,快来抢占沙发~
作者声明:非原创
平台声明:文章及评论仅代表作者本人观点,五略商书系信息发布平台,仅提供信息存储空间服务,如有侵权,请联系13521509118。转载请注明作者和出处:转自五略商书m.shangdiguo.com,作者商帝国咨询!